extending String call with a #copy!(n)

U

Une bévue

i've allready extendy String class by a #copy(n) like that :

class String
def copy(n)
out=""
(1..n).each {|i| out+=self}
return out
end
end

however i'd like to extend also String with a #copy!(n) (in place which
would work like that :

"*".copy!(4)
# => "****"

the prob is to affect self ???
 
A

Alex Young

Une said:
i've allready extendy String class by a #copy(n) like that :

class String
def copy(n)
out=""
(1..n).each {|i| out+=self}
return out
end
end

however i'd like to extend also String with a #copy!(n) (in place which
would work like that :

"*".copy!(4)
# => "****"

the prob is to affect self ???

Use << (or concat).
 
A

Alex Young

Une said:
with is the reason for this line ???

Your copy method produces identical results to the String#* method. For
example:

irb(main):001:0> a = "foo"
=> "foo"
irb(main):002:0> a * 3
=> "foofoofoo"
 
U

Une bévue

Alex Young said:
Your copy method produces identical results to the String#* method. For
example:

irb(main):001:0> a = "foo"
=> "foo"
irb(main):002:0> a * 3
=> "foofoofoo"

OK, fine, thanxs, i even didn't notice the ruby facility upon String
multiplication ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top