Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_key_as_attribute.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/hash_key_as_attribute.rb', line 4

def method_missing(symbol, *args)
  key = symbol.to_s
  if key.split('').last == "="
    self[key.chop.to_sym] = args[0]
  else
    unless self[symbol]
      "The key does not correspond to any hash entry"
    else
      self[symbol]
    end
  end
end