I have a class and a hash. How can I get the members of the hash to dynamically become methods on the class with the key as the method name?
class User
def initialize
@attributes = {"sn" => "Doe", "givenName" => "John"}
end
end
For example, I would like to be able to have the following output Doe:
u = User.new
puts u.sn
Explanation: If you call a method, which does not exist, method_missing is invoked with the name of the method as the first parameter, followed by the arguments given to the method and the block if one was given.
In the above we say that if a method, which was not defined, is called without arguments and without a block and the hash has an entry with the method name as key, it will return the value of that entry. Otherwise it will just proceed as usual.