- Published on
Using a Ruby Hash Like a Class Object
- Authors
- Name
- Iván González
- @dreamingechoes
If you ever need to use a Ruby Hash like a class object, you can achieve this with a simple trick:
class Hashit
def initialize(hash)
hash.each do |k,v|
self.instance_variable_set("@#{k}", v.is_a?(Hash) ? Hashit.new(v) : v)
self.class.send(:define_method, k, proc { self.instance_variable_get("@#{k}") })
self.class.send(:define_method, "#{k}=", proc { |v| self.instance_variable_set("@#{k}", v) })
end
end
end
h = Hashit.new({a: '123r', b: {c: 'sdvs'}})
# h.a => '123r'
# h.b.c => 'sdvs'
This approach allows you to dynamically create getter and setter methods for hash keys, effectively turning your hash into an object.
Extending Functionality
While this is a basic implementation, you can enhance the Hashit
class by adding methods to fit your specific needs.
Where Can I Find the Code?
You can find the full implementation on GitHub Gist. Feel free to modify and experiment with it! 😃