From: ara.t.howard@... Date: 2006-05-04T12:00:35+09:00 Subject: Re: Ruby idiom for attributes / properties On Thu, 4 May 2006, John Lam wrote: > I'm writing a bunch of auto-marshaling code to let CLR code call back into > Ruby. I just got the simplest possible marshaling scenario working today, > which is letting you bind an Array of ActiveRecord objects to a CLR > DataGridView control. See > http://www.iunknown.com/articles/2006/05/03/activerecord-and-windows-formsfor > a longer discussion and a screenshot. > > Bottom line is that I can now write: > > data_grid.data_source = Person.find_by_last_name('Lam') > > Now, here's my problem: I'm not all that happy that I'm special casing my > code for ActiveRecord objects. However, since Ruby doesn't have a mechanism > for runtime-discovery of attributes, I'm special casing for ActiveRecord > since it provides an @attributes hashtable with the name-value pairs for all > attributes in the object. This makes it easy for me to generate the CLR > anonymous class + object that implements the marshaling code that retrieves > the attribute data from the Ruby ActiveRecord object (think about this as > marshal by reference). > > Do folks have suggestions for implementing a discoverable attribute / > property idiom in Ruby? Perhaps via a mixin module in the standard library? > Or has this been done already and I'm just showing my Ruby newbiness? > > Thanks > -John > http://www.iunknown.com harp:~ > cat a.rb require 'traits' class C trait 'a' traits 'b' => 42, 'c' => 42.0 class_trait 'a' end p C.class_traits p C.traits harp:~ > ruby a.rb [["a", "a="]] [["a", "a="], ["b", "b="], ["c", "c="]] and a whole lot more http://rubyforge.org/projects/codeforpeople/ http://codeforpeople.com/lib/ruby/traits/traits-0.8.0/README regards. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama