Class: Ninja::Hash

Inherits:
Hash
  • Object
show all
Defined in:
lib/ninja/hash.rb

Instance Method Summary collapse

Methods inherited from Hash

#to_ninja_hash

Constructor Details

#initialize(hash) ⇒ Hash

Returns a new instance of Hash.



3
4
5
6
# File 'lib/ninja/hash.rb', line 3

def initialize hash
  self.replace hash
  hash.keys.each{|key| define(key) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ninja/hash.rb', line 21

def method_missing(name, *args)
  if md = name.to_s.match(/(.+)=\z/)
    define(md[1])
    self.send(name, *args)
  end
  nil
end

Instance Method Details

#[]=(key, value) ⇒ Object



8
9
10
11
# File 'lib/ninja/hash.rb', line 8

def []=(key, value)
  super(key, value)
  define(key)
end

#get(key, &block) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/ninja/hash.rb', line 13

def get(key, &block)
  unless value = self.send(key)
    block && block.call
  else
    value
  end
end