Sha256: 81341f276b3f27f32c3f48f5ad062ab29f98dc49f92a6afc76b165d529772c40
Contents?: true
Size: 404 Bytes
Versions: 13
Compression:
Stored size: 404 Bytes
Contents
class Hash # Returns a new hash containing only the keys specified # that exist in the current hash. # # {:a => '1', :b => '2', :c => '3'}.only(:a, :c) # # => {:a => '1', :c => '3'} # # Keys that do not exist in the original hash are ignored. def only(*keys) inject( {} ) do |new_hash, (key, value)| new_hash[key] = value if keys.include?(key) new_hash end end end
Version data entries
13 entries across 13 versions & 5 rubygems