# Defining attr like methods

**URL:** <https://www.ruby-forum.com/t/defining-attr-like-methods/135304>\
**Category:** Ruby\
**Created:** [April 20, 2008, 11:37pm UTC](https://www.ruby-forum.com/t/defining-attr-like-methods/135304 "2008-04-20T23:37:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chadmontplaisir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@chadmontplaisir](https://www.ruby-forum.com/u/chadmontplaisir)\
**Post date:** [April 20, 2008, 11:37pm UTC](https://www.ruby-forum.com/t/defining-attr-like-methods/135304/1 "2008-04-20T23:37:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![chadmontplaisir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@chadmontplaisir](https://www.ruby-forum.com/u/chadmontplaisir)\
**Post date:** [April 21, 2008, 12:11am UTC](https://www.ruby-forum.com/t/defining-attr-like-methods/135304/2 "2008-04-21T00:11:07Z")

</div>

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.

> 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

---

<div class="post-metadata">

**Author:** ![chadmontplaisir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@chadmontplaisir](https://www.ruby-forum.com/u/chadmontplaisir)\
**Post date:** [April 21, 2008, 1:09am UTC](https://www.ruby-forum.com/t/defining-attr-like-methods/135304/3 "2008-04-21T01:09:30Z")

</div>

Robert K. 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.

---

<div class="post-metadata">

**Author:** ![chadmontplaisir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@chadmontplaisir](https://www.ruby-forum.com/u/chadmontplaisir)\
**Post date:** [April 21, 2008, 2:36am UTC](https://www.ruby-forum.com/t/defining-attr-like-methods/135304/4 "2008-04-21T02:36:05Z")

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