A
Andrea Gianarro
Hello, I need to generate read and write accessors for all the keys in
an hash.
Say I have an attribute named my_attribute, I need "my_attribute" and
"my_attribute=" methods to read and write it to
@attributes[my_attribute].
I tried this code in the class constructor:
for a in all_attributes
method_name = a.name
self.class.class_eval do
define_method method_name do
@attributes[method_name]
end
define_method method_name+"=" do |val|
@attributes[method_name] = val
end
end
end
It generates the method for the class, but all the methods read and
write to the last attribute in the all_attributes list. That's
strange...
Thank you,
Andrea Gianarro
an hash.
Say I have an attribute named my_attribute, I need "my_attribute" and
"my_attribute=" methods to read and write it to
@attributes[my_attribute].
I tried this code in the class constructor:
for a in all_attributes
method_name = a.name
self.class.class_eval do
define_method method_name do
@attributes[method_name]
end
define_method method_name+"=" do |val|
@attributes[method_name] = val
end
end
end
It generates the method for the class, but all the methods read and
write to the last attribute in the all_attributes list. That's
strange...
Thank you,
Andrea Gianarro