"Joe Van Dyk" <joevandyk / gmail.com> schrieb im Newsbeitrag
news:c715e64050203113235bcec38 / mail.gmail.com...
> 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
>
>     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)

Did you consider using OpenStruct?

require 'ostruct'

my_obj = OpenStruct.new
my_obj.x_pos = 20
my_obj.z_pos = 40

assert(20, my_obj.x_pos)
assert(40, my_obj.z_pos)

Kind regards

    robert