Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hash-dot-evil.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *opts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hash-dot-evil.rb', line 18

def method_missing(method, *opts)
  m = method.to_s

  if m[-1] == '='
    define_writer(m)
    return self.send(method, *opts)
  else
    define_reader(m)
    return self.send(method, *opts)
  end
end

Instance Method Details

#define_reader(key) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/hash-dot-evil.rb', line 2

def define_reader(key)
  define_singleton_method(key.to_sym) do
    if has_key?(key)
      return self[key]
    else
      return self[key.to_sym]
    end
  end
end

#define_writer(key) ⇒ Object



12
13
14
15
16
# File 'lib/hash-dot-evil.rb', line 12

def define_writer(key)
  define_singleton_method(key.to_sym) do |value|
    self[key[0..-2].to_sym] = value
  end
end