From: Zach Dennis Date: 2005-10-31T22:36:35+09:00 Subject: Re: C# attributes in Ruby? Stephan Mueller wrote: > Hi Zach, > > * zdennis [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. Ok, still wrapping my head around this... Is the following a correct assumption? You would use: class MyObject ann :type => :browsable ... end Over something like? class CandyBar attr_reader :browseable? ... end Now perhaps I am missing something, so another question. During the lifetime of your object, would it's annotations change? So it may be browsable at one time during it's life, but then later on say the :type=>:browsable would be removed? Since you see annotations as so useful, I am just trying to make sure I see where the usefullness is, in case it is something I find would benefit my own code. That is why I ask so many questions. Zach