I have included this extension to Hash in my rails app...
class Hash
def method_missing(name, value=nil)
key = name.to_s.sub(/[=?!]$/,'').to_sym
self[key] = value if name.to_s[-1,1] == "="
return self[key]
end
end
I basically lets a plane old Hash behave like the fancy OpenStruct, so
you can do things like hash.amount = 10 instead of hash[:amount] =
10. It uses method_missing to do its stuff.
I have included this extension to Hash in my rails app...
class Hash
def method_missing(name, value=nil)
key = name.to_s.sub(/[=?!]$/,'').to_sym
self[key] = value if name.to_s[-1,1] == "="
return self[key]
end
end
I basically lets a plane old Hash behave like the fancy OpenStruct, so
you can do things like hash.amount = 10 instead of hash[:amount] =
10. It uses method_missing to do its stuff.
I suppose the danger is that the day you do hash.delete_if! instead of
getting a no method error it will just work and lead to a less easy to
understand error further down the road.