Defining attr like methods

Discussion in 'Ruby' started by Chad Murphy, Apr 20, 2008.

  1. Chad Murphy

    Chad Murphy Guest

    Here is what I'm trying to do

    class List
    # Method item
    # Pushes an item into an array @items
    # Method to_s
    # returns elements within @items into a string
    end

    class Groceries < List
    item :lettuce
    item :potato
    item :ham
    end

    print Groceries.new

    The trouble I'm having is figuring out how to go about adding an
    instance variable, which is an array, and will be updated using these
    attr like methods (I don't know what these are called which is giving me
    some trouble). Is this possible to pull off?
    --
    Posted via http://www.ruby-forum.com/.
     
    Chad Murphy, Apr 20, 2008
    #1
    1. Advertising

  2. On 20.04.2008 23:37, Chad Murphy wrote:
    > Here is what I'm trying to do
    >
    > class List
    > # Method item
    > # Pushes an item into an array @items
    > # Method to_s
    > # returns elements within @items into a string
    > end
    >
    > class Groceries < List
    > item :lettuce
    > item :potato
    > item :ham
    > end
    >
    > print Groceries.new
    >
    > The trouble I'm having is figuring out how to go about adding an
    > instance variable, which is an array, and will be updated using these
    > attr like methods (I don't know what these are called which is giving me
    > some trouble).


    You are looking for class methods.

    > Is this possible to pull off?


    Yes, but I doubt it is what you want: you are asking for class methods
    to add items but you create an instance (Groceries.new). Where is the
    point in defining a list of items on class level and instantiate it
    multiple times?

    If you describe what you want to achieve, i.e. what (business) problem
    you are trying to solve we can come up with other suggestions that may
    be more appropriate.

    For example: this looks like a case for inheritance:

    class Grocery
    end

    class Lettuce < Grocery
    end

    class Potato < Grocery
    end
    ....

    With a little bit of meta programming you can then get all the
    subclasses of Grocery.

    Kind regards

    robert
     
    Robert Klemme, Apr 20, 2008
    #2
    1. Advertising

  3. Chad Murphy

    Chad Murphy Guest

    Robert Klemme wrote:
    > On 20.04.2008 23:37, Chad Murphy wrote:
    >> item :lettuce
    >> item :potato
    >> item :ham
    >> end
    >>
    >> print Groceries.new
    >>
    >> The trouble I'm having is figuring out how to go about adding an
    >> instance variable, which is an array, and will be updated using these
    >> attr like methods (I don't know what these are called which is giving me
    >> some trouble).

    >
    > You are looking for class methods.


    Thanks.

    >> Is this possible to pull off?

    >
    > Yes, but I doubt it is what you want: you are asking for class methods
    > to add items but you create an instance (Groceries.new). Where is the
    > point in defining a list of items on class level and instantiate it
    > multiple times?
    >
    > If you describe what you want to achieve, i.e. what (business) problem
    > you are trying to solve we can come up with other suggestions that may
    > be more appropriate.
    >
    > For example: this looks like a case for inheritance:
    >
    > class Grocery
    > end
    >
    > class Lettuce < Grocery
    > end
    >
    > class Potato < Grocery
    > end
    > ...
    >
    > With a little bit of meta programming you can then get all the
    > subclasses of Grocery.


    I saw some code like this and tried playing around it. I think the
    problem was that I had no idea whether it was something you could do in
    order to abstract things from the sub class or something completely
    different, but I realized you could do that with a method.

    > Kind regards
    >
    > robert


    Thanks again.
    --
    Posted via http://www.ruby-forum.com/.
     
    Chad Murphy, Apr 21, 2008
    #3
  4. Hi,

    You might want to check out the Doodle rubygem at http://doodle.rubyforge.org/ .

    Here's an example of its use:

    daniel@daniel-desktop:~$ cat /tmp/doodle.rb
    require 'rubygems'
    require 'doodle'

    class List < Doodle::Base
    has :items, :collect => :item
    end

    class GroceryList < List
    # nothing added here.
    end

    my_list = GroceryList do
    item "Chunky bacon"
    item "Lettuce"
    item "Tomato"
    end

    p my_list.items

    daniel@daniel-desktop:~$ ruby /tmp/doodle.rb
    ["Chunky bacon", "Lettuce", "Tomato"]
    daniel@daniel-desktop:~$

    Dan

    On Sun, Apr 20, 2008 at 7:09 PM, Chad Murphy
    <> wrote:
    > Robert Klemme wrote:
    > > On 20.04.2008 23:37, Chad Murphy wrote:

    >
    > >> item :lettuce
    > >> item :potato
    > >> item :ham
    > >> end
    > >>
    > >> print Groceries.new
    > >>
    > >> The trouble I'm having is figuring out how to go about adding an
    > >> instance variable, which is an array, and will be updated using these
    > >> attr like methods (I don't know what these are called which is giving me
    > >> some trouble).

    > >
    > > You are looking for class methods.

    >
    > Thanks.
    >
    >
    > >> Is this possible to pull off?

    > >
    > > Yes, but I doubt it is what you want: you are asking for class methods
    > > to add items but you create an instance (Groceries.new). Where is the
    > > point in defining a list of items on class level and instantiate it
    > > multiple times?
    > >
    > > If you describe what you want to achieve, i.e. what (business) problem
    > > you are trying to solve we can come up with other suggestions that may
    > > be more appropriate.
    > >
    > > For example: this looks like a case for inheritance:
    > >
    > > class Grocery
    > > end
    > >
    > > class Lettuce < Grocery
    > > end
    > >
    > > class Potato < Grocery
    > > end
    > > ...
    > >
    > > With a little bit of meta programming you can then get all the
    > > subclasses of Grocery.

    >
    > I saw some code like this and tried playing around it. I think the
    > problem was that I had no idea whether it was something you could do in
    > order to abstract things from the sub class or something completely
    > different, but I realized you could do that with a method.
    >
    > > Kind regards
    > >
    > > robert

    >
    > Thanks again.
    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Daniel Finnie, Apr 21, 2008
    #4
    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. Ben Giddings

    "attr" like functions

    Ben Giddings, Jun 26, 2004, in forum: Ruby
    Replies:
    2
    Views:
    98
    Lennon Day-Reynolds
    Jun 28, 2004
  2. Sam Roberts
    Replies:
    3
    Views:
    158
    Sam Roberts
    Nov 14, 2004
  3. Gioele Barabucci

    class methods like attr:

    Gioele Barabucci, Jan 21, 2006, in forum: Ruby
    Replies:
    3
    Views:
    104
    Wilson Bilkovich
    Jan 23, 2006
  4. Kenneth McDonald
    Replies:
    5
    Views:
    353
    Kenneth McDonald
    Sep 26, 2008
  5. Marc Heiler

    Attr Methods and object setters

    Marc Heiler, Jun 25, 2009, in forum: Ruby
    Replies:
    25
    Views:
    238
    Brian Candler
    Jun 29, 2009
Loading...

Share This Page