Hi Zach,

* zdennis <zdennis / mktec.com> [051031 03:45]:

> I am trying to see where you would use? Is the below example valid of how-you would use it?
> 
> arr = [ obj1, obj2, obj3, obj4 ]
> arr.each do |x|
>     puts obj1.value if obj1.ann.fancy_attribute =~ /fancy_value/
> end
> 
> OR
> 
> fancy_objects = 0
> ObjectSpace.each_object{ |obj| fancy_objects += 1 if obj.ann.fancy_attribute =~ /fancy_value }
> puts "YOu had #{fancy_object} fancy objects"

yes, this are valid applications of attributes. Another popular example
would be the "browsable" attribute used in the .net framework. It is used
to annotate properties (in Ruby this would be attributes, the choice of
names is a little bit confusing in this case). If you have some generic
visual control like a listbox, you can now attach a collection of any
type of objects to the listbox and the listbox could decide which of the
properties it should display in form of a column (of course the
properties which have the attribute browsable attached).

In my opinion this a quite useful feature to implement very flexible oo
code.


Cheers,

Steph.