From: Brian Candler Date: 2009-12-15T20:35:32+09:00 Subject: Re: meta-programming Jes炭s Gabriel y Gal叩n wrote: > irb(main):051:0> C.add_method(:name) {@name} > => # > irb(main):052:0> C.add_method(:name=) {|value| @name = value} > => # > irb(main):053:0> c = C.new > => # > irb(main):054:0> c.name=3 > => 3 > irb(main):055:0> c.name > => 3 Of course, since 'name' is static here, you could just include a module. It gets more fun when the names of the accessors themselves are dynamic. At this point it really does make more sense to use string eval, rather than the alternative (instance_variable_get and instance_variable_set). Have a look at define_method_attribute= in the following: http://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods/write.rb Also see define_read_method in attribute_methods/read.rb, and method_missing in base.rb (this defines finder methods dynamically) -- Posted via http://www.ruby-forum.com/.