On Feb 3, 2005, at 11:33 AM, 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.

class SomeObject
     def initialize
         @attributes = { :x_pos => 20, :y_pos => 30, :z_pos => 40 }
     end

     def method_missing(meth, *args) # notice rename - don't like having 
vars the same as methods
         # test to see if this is a get or set method
         if meth =~ /^(.+)=$/
             set($1.intern, args)
         else
             get(meth)
         end
     end

     def get(attribute)
         @attributes[attribute]
     end

     def set(attribute, value)
         # you might also want to check at this stage that the attribute 
exists
         @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)
--
ryand-ruby / zenspider.com - Seattle.rb - 
http://www.zenspider.com/seattle.rb
http://blog.zenspider.com/ - http://rubyforge.org/projects/ruby2c