# Defining attr like methods

**URL:** <https://rubytalk.org/t/defining-attr-like-methods/45992>\
**Category:** ruby-talk\
**Created:** [20 April 2008 21:37 UTC](https://rubytalk.org/t/defining-attr-like-methods/45992 "2008-04-20T21:37:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chad\_Murphy](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Chad\_Murphy](https://rubytalk.org/u/Chad_Murphy)\
**Post date:** [20 April 2008 21:37 UTC](https://rubytalk.org/t/defining-attr-like-methods/45992/1 "2008-04-20T21:37:38Z")

</div>

Here is what I'm trying to do

class List  
&nbsp;&nbsp;# Method item  
&nbsp;&nbsp;# Pushes an item into an array @items  
&nbsp;&nbsp;# Method to\_s  
&nbsp;&nbsp;# returns elements within @items into a string  
end

class Groceries \< List  
&nbsp;&nbsp;item :lettuce  
&nbsp;&nbsp;item :potato  
&nbsp;&nbsp;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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [20 April 2008 22:10 UTC](https://rubytalk.org/t/defining-attr-like-methods/45992/2 "2008-04-20T22:10:10Z")

</div>

> Here is what I'm trying to do
> 
> class List  
> &nbsp;&nbsp;# Method item  
> &nbsp;&nbsp;# Pushes an item into an array @items  
> &nbsp;&nbsp;# Method to\_s  
> &nbsp;&nbsp;# returns elements within @items into a string  
> end
> 
> class Groceries \< List  
> &nbsp;&nbsp;item :lettuce  
> &nbsp;&nbsp;item :potato  
> &nbsp;&nbsp;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

&nbsp;&nbsp;robert

> **···**
>
> On 20.04.2008 23:37, Chad Murphy wrote:

---

<div class="post-metadata">

**Author:** ![Chad\_Murphy](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Chad\_Murphy](https://rubytalk.org/u/Chad_Murphy)\
**Post date:** [20 April 2008 23:09 UTC](https://rubytalk.org/t/defining-attr-like-methods/45992/3 "2008-04-20T23:09:55Z")

</div>

Robert Klemme wrote:

> > &nbsp;&nbsp;item :lettuce  
> > &nbsp;&nbsp;item :potato  
> > &nbsp;&nbsp;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
> 
> &nbsp;&nbsp;robert

Thanks again.

> **···**
>
> > On 20.04.2008 23:37, Chad Murphy wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Daniel\_Finnie](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Daniel\_Finnie](https://rubytalk.org/u/Daniel_Finnie)\
**Post date:** [21 April 2008 00:35 UTC](https://rubytalk.org/t/defining-attr-like-methods/45992/4 "2008-04-21T00:35:26Z")

</div>

Hi,

You might want to check out the Doodle rubygem at [http://doodle.rubyforge.org/](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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has :items, :collect =\> :item  
end

class GroceryList \< List  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# nothing added here.  
end

my\_list = GroceryList do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item "Chunky bacon"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item "Lettuce"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 \<floppyformattingfrenzy@gmail.com\> 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/\](http://www.ruby-forum.com/%5C).
