From: Joe Van Dyk Date: 2005-02-04T04:51:29+09:00 Subject: Re: method_missing question On Thu, 3 Feb 2005 11:32:53 -0800, Joe Van Dyk wrote: > Can someone complete the psuedo-code in SomeObject#method_missing for > me? Or suggest a better way of doing this? The attributes for > SomeObject are, in my application, are generated from a configuration > file. > > Thanks > Joe > > class SomeObject > def initialize > @attributes = { :x_pos => 20, :y_pos => 30, :z_pos => 40 } > end > > def method_missing(method, *args) > # test to see if this is a get or set method > if method contains an = > set(method minus the equal, args) > else > get(method) > end > end Here's what I have so far. This works, but wondering if something better's out there. def method_missing(method, *args) puts "method missing called with #{method} and #{args}" if method.to_s =~ /^(.+)=$/ set($1.to_sym, *args) else get(method.to_sym) end end > > def get(attribute) > @attributes[attribute] > end > > def set(attribute, value) > @attributes[attribute] = value > end > end > > my_obj = SomeObject.new > my_obj.x_pos = 20 > my_obj.z_pos = 40 > > assert(20, my_obj.x_pos) > assert(40, my_obj.z_pos) >