Class: Hashit
- Inherits:
-
Object
- Object
- Hashit
- Defined in:
- lib/hashit.rb
Instance Method Summary collapse
-
#initialize(hash) ⇒ Hashit
constructor
A new instance of Hashit.
Constructor Details
#initialize(hash) ⇒ Hashit
Returns a new instance of Hashit.
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/hashit.rb', line 2 def initialize(hash) hash.each do |k,v| if v.class == "Array" && v.size == 1 v = v.first end self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable end end |