Module: JHash

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

Overview

Wednesday, August 19 2020

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, arg = :nothing) ⇒ Object

rubocop:disable Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jhash.rb', line 7

def method_missing(method_name, arg = :nothing) # rubocop:disable Metrics/MethodLength
  if arg.eql? :nothing
    return self[method_name]
  elsif method_name.to_s.match?(/\w+\d*=/)
    m_name = method_name[/[[:alnum:]]+/]
    return merge!({ m_name.to_sym => arg})
  end
  obj = self[method_name]
  begin
    msg = obj.send(arg)
  rescue NoMethodError => err
    msg = err.message
  end
  msg
end