Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/ys1/hash_to_class.rb
Overview
OpenClass Hash
Instance Method Summary collapse
-
#to_anon_class ⇒ Class
Builds an anonymous class from the hash.
Instance Method Details
#to_anon_class ⇒ Class
Builds an anonymous class from the hash.
Each hash key becomes an attribute with both reader and writer methods. The corresponding values are assigned when an instance is initialized.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ys1/hash_to_class.rb', line 22 def to_anon_class source = self Class.new do source.each_key { |key| attr_accessor key.to_sym } define_method(:initialize) do source.each do |key, value| instance_variable_set("@#{key}", value) end end end end |