Class: Hash

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

Overview

Instance Method Summary collapse

Instance Method Details

#slice(keys = []) ⇒ Object

Returns a new hash with only the given keys.



5
6
7
8
9
10
11
12
# File 'lib/core_ext/hash.rb', line 5

def slice(keys = [])
  h = {}
  keys.each do |key|
    raise IndexError if self[key].nil?
    h[key] = self[key]
  end
  h
end