From: John Lam Date: 2006-05-04T22:35:26+09:00 Subject: Re: Ruby idiom for attributes / properties ------=_Part_3137_4487726.1146749716990 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi Dave, Duck typing for attributes - cool idea, but I worry about it because of the difficulty (impossibility) in telling read-only properties from ordinary methods: module Utilities def format_disk end end class Person include Utilities def first_name end end You see my interest in this topic is driven entirely around my ability to add some runtime magic in my marshaler. Since the metadata doesn't exist in Ruby, I have to rely on some kind of convention. I might need to add that convention via a mixin that you can write that serves as an adapter between the type that you want to make data-bindable in .NET. So you could patch ActiveRecord in a RubyCLR app via: class ActiveRecord include RubyClr::MakeBindable end This removes the hard-coded depenency on ActiveRecord from my marshaler. Does this sound reasonable? Thanks -John http://www.iunknown.com On 5/4/06, Dave Burt wrote: > > Another approach might be to simply narrow down what valid "attribute" > methods look like. Does it have to have a foo=3D() and foo() pair? Does > foo() have to be parameterless, or is it accept optional parameters > (like Ara's traits)? Then you can just pull those methods out of the > class: > > class Module > def attributes > instance_methods.select do |name| > next if name =3D~ /!$/ || name =3D~ /=3D$/ > setter =3D name.sub(/\?$/, "") + "=3D" > instance_methods.include?(setter) && > [0, -1].include?(instance_method(name).arity) && > [1, -2].include?(instance_method(setter).arity) > end > end > end > > struct =3D Struct.new(:foo, :bar) > [Dir, File, IO, Hash, Thread, Struct::Tms, struct].each do |c| > p [c, c.attributes] > end > > =3D> [Dir, ["pos"]] > [File, ["sync", "lineno", "pos"]] > [IO, ["sync", "lineno", "pos"]] > [Hash, ["default"]] > [Thread, ["abort_on_exception", "priority"]] > [Struct::Tms, ["cutime", "cstime", "stime", "utime"]] > [#, ["bar", "foo"]] > > Cheers, > Dave > > ------=_Part_3137_4487726.1146749716990--