If you're just trying to get a working Expando
for use, use OpenStruct
instead. But if you're doing this for educational value, let's fix the bugs.
The arguments to method_missing
When you call person.name = "Michael"
this is translated into a call to person.method_missing(:name=, "Michael")
, so you don't need to pull the parameter out with a regular expression. The value you're assigning is a separate parameter. Hence,
if method_id.to_s[-1,1] == "=" #the last character, as a string
name=method_id.to_s[0...-1] #everything except the last character
#as a string
#We'll come back to that class_eval line in a minute
#We'll come back to the instance_variable_set line in a minute as well.
else
super.method_missing(method_id, *arguments)
end
instance_variable_set
Instance variable names all start with the @
character. It's not just syntactic sugar, it's actually part of the name. So you need to use the following line to set the instance variable:
instance_variable_set("@#{name}", arguments[0])
(Notice also how we pulled the value we're assigning out of the arguments
array)
class_eval
self.class
refers to the Expando
class as a whole. If you define an attr_accessor
on it, then every expando will have an accessor for that attribute. I don't think that's what you want.
Rather, you need to do it inside a class << self
block (this is the singleton class or eigenclass of self
). This operates inside the eigenclass for self
.
So we would execute
class << self; attr_accessor name.to_sym ; end
However, the variable name
isn't actually accessible inside there, so we're going to need to single out the singleton class first, then run class_eval
. A common way to do this is to out this with its own method eigenclass
So we define
def eigenclass
class << self; self; end
end
and then call self.eigenclass.class_eval { attr_accessor name.to_sym }
instead)
The solution
Combine all this, and the final solution works out to
class Expando
def eigenclass
class << self; self; end
end
def method_missing(method_id, *arguments)
if method_id.to_s[-1,1] == "="
name=method_id.to_s[0...-1]
eigenclass.class_eval{ attr_accessor name.to_sym }
instance_variable_set("@#{name}", arguments[0])
else
super.method_missing(method_id, *arguments)
end
end
end