Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > extending String call with a #copy!(n)

Reply
Thread Tools

extending String call with a #copy!(n)

 
 
Une bévue
Guest
Posts: n/a
 
      08-10-2006

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 ???
--
une bévue
 
Reply With Quote
 
 
 
 
Alex Young
Guest
Posts: n/a
 
      08-10-2006
Une bévue wrote:
> 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).

--
Alex

 
Reply With Quote
 
 
 
 
Alex Young
Guest
Posts: n/a
 
      08-10-2006
Alex Young wrote:
> Une bévue wrote:
>> 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).
>

or sub!.

class String
def copy!(n)
self.sub!(self, self*n)
end
end

--
Alex

 
Reply With Quote
 
Daniel DeLorme
Guest
Posts: n/a
 
      08-10-2006
Alex Young wrote:
>> Use << (or concat).
>>

> or sub!.


replace would be more efficient:
class String
alias copy *
def copy!(n)
replace(self*n)
end
end

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      08-10-2006
Daniel DeLorme wrote:
> Alex Young wrote:
>>> Use << (or concat).
>>>

>> or sub!.

>
> replace would be more efficient:
> class String
> alias copy *
> def copy!(n)
> replace(self*n)
> end
> end
>

Oh yes. So it would

--
Alex

 
Reply With Quote
 
Une bévue
Guest
Posts: n/a
 
      08-10-2006
Alex Young <(E-Mail Removed)> wrote:

> Oh yes. So it would


thanxs to all !
--
une bévue
 
Reply With Quote
 
Une bévue
Guest
Posts: n/a
 
      08-10-2006
Daniel DeLorme <(E-Mail Removed)> wrote:

> alias copy *


with is the reason for this line ???
--
une bévue
 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      08-10-2006
Une bévue wrote:
> Daniel DeLorme <(E-Mail Removed)> wrote:
>
>> alias copy *

>
> 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"

--
Alex

 
Reply With Quote
 
Une bévue
Guest
Posts: n/a
 
      08-10-2006
Alex Young <(E-Mail Removed)> wrote:

>
> 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
--
une bévue
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extending String jk Java 13 08-05-2008 02:31 PM
java.util.Properties extending from HashMap<Object, Object> insteadof HashMap<String, String> Rakesh Java 10 04-08-2008 04:22 AM
Packing a simple dictionary into a string - extending struct? Jonathan Fine Python 9 06-22-2007 11:26 PM
compilation error: "error: no matching function for call to 'String::String(String)' =?ISO-8859-1?Q?Martin_J=F8rgensen?= C++ 5 05-06-2006 03:48 PM
Extending string class Carl Youngblood C++ 4 07-10-2004 01:48 AM



Advertisments