Hash#zip method for ruby
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
Gemfile
README.md
Rakefile
hash-zip.gemspec

README.md

Implements a much needed Hash#zip method.

To install run gem install hash-zip, and then require "hash-zip" in your code.

Usage

You can zip multiple Hashes. Keys in result will be keys in any of the inputs, and values will be Arrays with missing values padded by nils.

a = {x: 1, y: 2}
b = {y: 3, z: 4}
a.zip(b)
# returns {x: [1, nil], y: [2, 3], z: [nil, 4]}

You can also use it with a block. In such case the return value is nil:

a = {x: 1, y: 2}
b = {y: 3, z: 4}
a.zip(b) do |key, value_a, value_b|
  p [key, value_a, value_b]
  # Yields [:x, 1, nil]
  #        [:y, 2, 3]
  #        [:z, nil, 4]
end

It works with more than one argument (a.zip(b,c)) as well as with zero arguments (a.zip()).

If arguments are not Hashes, but accept to_h, they will be converted automatically before zipping.