Sha256: 9e0643651420d0d99c0b2adc06128899c04d618f22205c728fdacb4ae752dd00

Contents?: true

Size: 307 Bytes

Versions: 7

Compression:

Stored size: 307 Bytes

Contents

class Hash
  # Returns a new hash only with the specified keys (if present).
  #   hash = { a: 1, b: 2, c: 3 }
  #   hash.extract(:b, :a)
  #   #=> { a: 1, b: 2 }
  #   hash.extract(:a, :d)
  #   #=> { a: 1}
  def extract(*ks)
    existing = keys & ks
    Hash[existing.zip(values_at(*existing))]
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
shenanigans-1.0.13 lib/shenanigans/hash/extract.rb
shenanigans-1.0.11 lib/shenanigans/hash/extract.rb
shenanigans-1.0.10 lib/shenanigans/hash/extract.rb
shenanigans-1.0.9 lib/shenanigans/hash/extract.rb
shenanigans-1.0.8 lib/shenanigans/hash/extract.rb
shenanigans-1.0.7 lib/shenanigans/hash/extract.rb
shenanigans-1.0.6 lib/shenanigans/hash/extract.rb