This is what I use (I call this idiom hash init).
def initialize(object_attribute_hash = {})
object_attribute_hash.map { |(k, v)| send("#{k}=", v) }
end
If you're on Ruby 1.9, you can make it even cleaner (sending allows private methods):
def initialize(object_attribute_hash = {})
object_attribute_hash.map { |(k, v)| public_send("#{k}=", v) }
end
NoMethodError, foo "foo =" . ( attrs, ),
def initialize(object_attribute_hash = {})
object_attribute_hash.map do |(k, v)|
writer_m = "#{k}="
send(writer_m, v) if respond_to?(writer_m) }
end
end
, (, ), , , - . NoMethodError - ( ).
( ),
some_object.methods.grep(/\w=$/)
grep , .
eval("@#{opt} = \"#{val}\"")
val - - , .