Xavier
Xavier

Reputation: 323

Zipping uneven arrays in ruby

I have two uneven arrays:

a = [1,2,3,4,5,6,7,8,9,10]
b = ['d','e','f','g','h','i','j']

I'd like to bind them so that the object returned looks like this:

result = [[nil, 1], [nil, 2], [nil, 3], ["d", 4], ["e", 5], ["f", 6], ["g", 7],
["h", 8], ["i", 9], ["j", 10]] 

The zip method would not work, as it does the opposite by lining up elements to the front. So far I have:

def bind(a,b,ac=a.count,bc=b.count)
  distance = ac - bc < 0 ? bc - ac : ac - bc
  min = ac > bc ? b : a
  max = ac > bc ? a : b 
  distance.times { min.unshift(nil) }
  return min.zip(max)
end

Does ruby have a method to resolve this (or a quicker way to go about it)?

Upvotes: 2

Views: 1039

Answers (3)

hirolau
hirolau

Reputation: 13921

Although very close to Cary's solution. I find this more readable.

missing_elements = Array.new(a.size-b.size)
p (missing_elements+b).zip(a) 

Upvotes: 0

Cary Swoveland
Cary Swoveland

Reputation: 110725

Assuming a.size >= b.size:

([nil]*(a.size-b.size)).concat(b).zip(a)
  #=> [[nil, 1], [nil, 2], [nil, 3], ["d", 4], ["e", 5],
  #    ["f", 6], ["g", 7], ["h", 8], ["i", 9], ["j", 10]] 

or

[([nil]*(a.size-b.size)).concat(b), a].transpose

The methods Enumerable#zip and Array#transpose are yin and yang when the two arrays are the same size.

Upvotes: 3

sawa
sawa

Reputation: 168199

What about this?

a.reverse.zip(b.reverse).reverse.map(&:reverse)

Upvotes: 2

Related Questions