From: ara.t.howard@... Date: 2006-11-12T12:20:47+09:00 Subject: Re: Can Ruby do this magic... elegantly On Sun, 12 Nov 2006, J2M wrote: > I would like to be able to be able to include instance methods of a > Struct into a class. e.g. > > foo = Struct.new(:attribute, :another_attribute) > bar = foo.new > > class Bas > > some_ruby_magic > > end > > So that I can then do > > Bas.attribute="a value" > Bas.attribute > > Kind of like doing module_functions but that doesn't work inside a > class. > Thanks, > James harp:~ > cat a.rb class Module def struct_attrs struct singleton_class = class << self self end singleton_class.module_eval{ struct.members.each{|m| attr_accessor m} } end end class Bas foo = Struct.new :attribute, :another_attribute bar = foo.new struct_attrs foo end Bas.attribute = "a value" p Bas.attribute harp:~ > ruby a.rb "a value" -a -- my religion is very simple. my religion is kindness. -- the dalai lama