Class: DOH
Instance Method Summary collapse
-
#initialize(initial_hash = {}, &block) ⇒ DOH
constructor
Initialize with an optional hash and a block.
-
#method_missing(method_name, *args, &block) ⇒ Object
Override method_missing to pass undefined methods to the parent Hash.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Ensure respond_to_missing? is correctly implemented.
Methods inherited from Hash
#pub_name, #sort_by_key, #sym_keys
Constructor Details
#initialize(initial_hash = {}, &block) ⇒ DOH
Initialize with an optional hash and a block
8 9 10 11 12 |
# File 'lib/doh.rb', line 8 def initialize(initial_hash = {}, &block) super() # Initialize the parent Hash self.merge!(initial_hash) # Merge the initial hash into the current hash self.default_proc = block if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Override method_missing to pass undefined methods to the parent Hash
15 16 17 18 19 20 21 22 |
# File 'lib/doh.rb', line 15 def method_missing(method_name, *args, &block) # sc = caller[0].split; pp ['DOH.mm',method_name,sc[-1],sc[-2].split('/')[-1].split(':')[0..1].join(':')] if Hash.instance_methods.include?(method_name) super else raise NoMethodError, "undefined method `#{method_name}` for #{self}:#{self.class}" end end |
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Ensure respond_to_missing? is correctly implemented
25 26 27 28 |
# File 'lib/doh.rb', line 25 def respond_to_missing?(method_name, include_private = false) # sc = caller[0].split; pp ['DOH.rtm?',method_name,sc[-1],sc[-2].split('/')[-1].split(':')[0..1].join(':')] Hash.instance_methods.include?(method_name) || super end |