Extending Array and assigning to self

Discussion in 'Ruby' started by Oliver Saunders, May 6, 2008.

  1. I've got this class that is essentially an array with a few things
    added. Here's some of it:

    class Pages

    def initialize
    @content = []
    end

    def import(file, page_delimiter = ' ')
    @content = file.read.split page_delimiter
    self
    end

    def [](num)
    @content[num]
    end

    def <<(page_content)
    @content << page_content
    end

    ....

    Because I'm basically reimplementing Array I thought it might me more
    sense to inherit from Array or delegate to Array. The trouble is that I
    need to assign to @content. How do you suggest I get round this problem?
    --
    Posted via http://www.ruby-forum.com/.
    Oliver Saunders, May 6, 2008
    #1
    1. Advertising

  2. Oliver Saunders

    yermej Guest

    On May 5, 6:21 pm, Oliver Saunders <> wrote:
    > I've got this class that is essentially an array with a few things
    > added. Here's some of it:
    >
    > class Pages
    >
    > def initialize
    > @content = []
    > end
    >
    > def import(file, page_delimiter = ' ')
    > @content = file.read.split page_delimiter
    > self
    > end
    >
    > def [](num)
    > @content[num]
    > end
    >
    > def <<(page_content)
    > @content << page_content
    > end
    >
    > ...
    >
    > Because I'm basically reimplementing Array I thought it might me more
    > sense to inherit from Array or delegate to Array. The trouble is that I
    > need to assign to @content. How do you suggest I get round this problem?
    > --
    > Posted viahttp://www.ruby-forum.com/.


    I would just use Array's own methods. E.g.:
    class Pages < Array
    def import(file, page_delimiter = ' ')
    self.clear
    self.concat file.read.split.page_delimiter
    end
    end

    I'm not sure if Array#concat is the best choice here (for some
    definition of best), but it'll work.
    yermej, May 6, 2008
    #2
    1. Advertising

  3. yermej wrote:
    > I would just use Array's own methods. E.g.:
    > class Pages < Array
    > def import(file, page_delimiter = ' ')
    > self.clear
    > self.concat file.read.split.page_delimiter
    > end
    > end
    >
    > I'm not sure if Array#concat is the best choice here (for some
    > definition of best), but it'll work.


    Fantastic yermej. I didn't know about that concat method.

    Next point: What if I have a similar problem with strings? I'd like to
    achieve this:

    # is a string
    x = ValueChangeString.new 'foo' #=> 'foo'
    # has all the methods of string already
    x.length #=> 3
    # But also has methods that completely alter the string's value
    # whilst remaining the same object
    x.change_value! #=> 'bar'
    --
    Posted via http://www.ruby-forum.com/.
    Oliver Saunders, May 6, 2008
    #3
  4. Hi --

    On Tue, 6 May 2008, yermej wrote:

    > On May 5, 6:21 pm, Oliver Saunders <> wrote:
    >> I've got this class that is essentially an array with a few things
    >> added. Here's some of it:
    >>
    >> class Pages
    >>
    >> def initialize
    >> @content = []
    >> end
    >>
    >> def import(file, page_delimiter = ' ')
    >> @content = file.read.split page_delimiter
    >> self
    >> end
    >>
    >> def [](num)
    >> @content[num]
    >> end
    >>
    >> def <<(page_content)
    >> @content << page_content
    >> end
    >>
    >> ...
    >>
    >> Because I'm basically reimplementing Array I thought it might me more
    >> sense to inherit from Array or delegate to Array. The trouble is that I
    >> need to assign to @content. How do you suggest I get round this problem?
    >> --
    >> Posted viahttp://www.ruby-forum.com/.

    >
    > I would just use Array's own methods. E.g.:
    > class Pages < Array
    > def import(file, page_delimiter = ' ')
    > self.clear
    > self.concat file.read.split.page_delimiter


    That's actually a space before page_delimiter, not a dot. Or:

    file.read.split(page_delimiter)

    just to be sure :)

    > end
    > end
    >
    > I'm not sure if Array#concat is the best choice here (for some
    > definition of best), but it'll work.


    In general I'd use #replace rather than #clear plus #concat.


    David

    --
    Rails training from David A. Black and Ruby Power and Light:
    INTRO TO RAILS June 9-12 Berlin
    ADVANCING WITH RAILS June 16-19 Berlin
    INTRO TO RAILS June 24-27 London (Skills Matter)
    See http://www.rubypal.com for details and updates!
    David A. Black, May 6, 2008
    #4
  5. > In general I'd use #replace rather than #clear plus #concat.
    >
    >
    > David


    Whoa! That answers the string question. Thanks :)
    --
    Posted via http://www.ruby-forum.com/.
    Oliver Saunders, May 6, 2008
    #5
    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. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    579
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    594
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    399
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    374
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    402
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page