extending String call with a #copy!(n)

Discussion in 'Ruby' started by Une bévue, Aug 10, 2006.

  1. Une bévue

    Une bévue Guest

    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
     
    Une bévue, Aug 10, 2006
    #1
    1. Advertising

  2. Une bévue

    Alex Young Guest

    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
     
    Alex Young, Aug 10, 2006
    #2
    1. Advertising

  3. Une bévue

    Alex Young Guest

    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
     
    Alex Young, Aug 10, 2006
    #3
  4. 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
     
    Daniel DeLorme, Aug 10, 2006
    #4
  5. Une bévue

    Alex Young Guest

    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
     
    Alex Young, Aug 10, 2006
    #5
  6. Une bévue

    Une bévue Guest

    Alex Young <> wrote:

    > Oh yes. So it would :)


    thanxs to all !
    --
    une bévue
     
    Une bévue, Aug 10, 2006
    #6
  7. Une bévue

    Une bévue Guest

    Daniel DeLorme <> wrote:

    > alias copy *


    with is the reason for this line ???
    --
    une bévue
     
    Une bévue, Aug 10, 2006
    #7
  8. Une bévue

    Alex Young Guest

    Une bévue wrote:
    > Daniel DeLorme <> 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
     
    Alex Young, Aug 10, 2006
    #8
  9. Une bévue

    Une bévue Guest

    Alex Young <> 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
     
    Une bévue, Aug 10, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,254
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,315
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  3. Replies:
    26
    Views:
    2,140
    Roland Pibinger
    Sep 1, 2006
  4. Replies:
    2
    Views:
    2,262
    John Harrison
    May 26, 2007
  5. Rakesh
    Replies:
    10
    Views:
    12,212
    Mike Schilling
    Apr 8, 2008
Loading...

Share This Page