Method: Hash#only

Defined in:
lib/extlib/hash.rb

#only(*allowed) ⇒ Hash

Create a hash with only key/value pairs in receiver and allowed

{ :one => 1, :two => 2, :three => 3 }.only(:one)    #=> { :one => 1 }

Parameters:

Returns:

  • (Hash)

    A new hash with only the selected keys.



167
168
169
170
171
# File 'lib/extlib/hash.rb', line 167

def only(*allowed)
  hash = {}
  allowed.each {|k| hash[k] = self[k] if self.has_key?(k) }
  hash
end