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
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
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.
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
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
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
Perhaps you could do it simpler by changing this logic? I was thinking along the lines of
def initialize()
{ :x_pos => 20, :y_pos => 30, :z_pos => 40 }.each do |key, value|
instance_variable_set("@#{key}", value)
class << self; self; end.send(:attr_accessor, key)
end
end
But that won't quite work when the field set can change after initialize has been called. When you know all the keys upfront you can also move the accessors to the class definition.
That looks fine to me. I, by habit/musclememory, use intern instead of to_sym. You shouldn't need to do it for your get as method_missing should be passed a symbol in the first place.
···
On Feb 3, 2005, at 11:51 AM, Joe Van Dyk wrote:
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
> 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
That looks fine to me. I, by habit/musclememory, use intern instead of
to_sym. You shouldn't need to do it for your get as method_missing
should be passed a symbol in the first place.