Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/forrst/monkeys/hash.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#subset(*keys) ⇒ Hash

Given a set of keys this method will create a new hash with those keys and their values.

Examples:

{:name => "Chuck Norris", :age => 71}.subset(:age) # => {:age => 71}

Parameters:

  • keys (Array)

    The keys to retrieve from the hash.

Returns:

Author:

  • Yorick Peterse

Since:

  • 0.1a



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/forrst/monkeys/hash.rb', line 15

def subset(*keys)
  new_hash = {}

  keys.each do |k|
    if self.key?(k)
      new_hash[k] = self[k]
    end
  end
  
  return new_hash
end