Module: HashUtilsNamedAttributes

Included in:
Hash
Defined in:
lib/hashutils.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hashutils.rb', line 151

def method_missing(m, *args, &block)
  m_to_s = m.to_s
  is_setter = (m_to_s[m_to_s.length-1..m_to_s.length-1] == "=")
  sym = (is_setter) ? m_to_s[0..m_to_s.length-2].to_sym : m
  if is_setter
    self[sym] = args[0]
  else
    if self[sym]
      out = self[sym]
    else
      raise NoMethodError, "undefined method `#{m_to_s}' for Hash"
    end
  end
end