Module: Monkey::Hash::SubHash

Defined in:
lib/monkey/hash/sub_hash.rb

Overview

Adds sub hash ablilty (like Hash#[], but returning a hash rather than an array).

Instance Method Summary collapse

Instance Method Details

#sub_hash(*keys) ⇒ Object Also known as: subhash

Example:

{:a => 42, :b => 23, :c => 1337}.sub_hash :a, :b, :d
# => {:a => 42, :b => 23}


12
13
14
15
16
17
# File 'lib/monkey/hash/sub_hash.rb', line 12

def sub_hash(*keys)
  keys.inject({}) do |hash, key|
    hash.merge! key => self[key] if include? key
    hash
  end
end