0.0
No release in over 3 years
Low commit activity in last 3 years
New Hash methods from 2.4/2.5 and well as Hash convenience methods from the future
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
>= 0
 Project Readme

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()).

You can also use the zip_compact variant which discards nil values:

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

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