Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/ys1/core_ext/hash.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 |
# File 'lib/ys1/core_ext/hash.rb', line 22 def to_anon_class hash = self Class.new do hash.each_key { |key| attr_accessor key } define_method(:initialize) do hash.each { |key, value| instance_variable_set("@#{key}", value) } end end end |