Class: Metash
- Inherits:
-
Object
- Object
- Metash
- Defined in:
- lib/metash.rb
Overview
A hash with method_missing implemented so you can make calls i.e.: hash.foo.bar => hash[:bar]
hash.foo "bar" => hash[:foo] = "bar"
Instance Method Summary collapse
-
#__hash ⇒ Object
Returns the internal hash, it may be useful.
-
#initialize(hash = {}) ⇒ Metash
constructor
A new instance of Metash.
-
#inspect ⇒ Object
A Hash-like inspect.
-
#method_missing(meth, *args) ⇒ Object
Some method_missing magic is used to provide the nice interface.
- #orig_method_missing ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Metash
Returns a new instance of Metash.
11 12 13 |
# File 'lib/metash.rb', line 11 def initialize(hash={}) @hash = recurse(hash) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
Some method_missing magic is used to provide the nice interface.
18 19 20 21 22 23 24 25 |
# File 'lib/metash.rb', line 18 def method_missing(meth, *args) return nil if !@hash.key?(meth) && args.empty? if args.empty? @hash[meth] else @hash[meth] = args.size == 1 ? args.first : args end end |
Instance Method Details
#__hash ⇒ Object
Returns the internal hash, it may be useful
28 29 30 |
# File 'lib/metash.rb', line 28 def __hash @hash end |
#inspect ⇒ Object
A Hash-like inspect
33 34 35 36 37 38 39 |
# File 'lib/metash.rb', line 33 def inspect strs = [] @hash.each do |key, val| strs << "%s: %s" % [key, val.inspect] end "{%s}" % strs.join(', ') end |
#orig_method_missing ⇒ Object
15 |
# File 'lib/metash.rb', line 15 alias_method :orig_method_missing, :method_missing |