Convert ["a", nil, "b", "c", nil] and ["d", nil, "e", "f"] into ["af", "be", "cd"] using Ruby in one sentence Hint: Remember, Do it in one sentence.
CodePudding user response:
I would go with:
a = ["a", nil, "b", "c", nil]
b = ["d", nil, "e", "f"]
a.compact.zip(b.compact.reverse).map(&:join)
#=> ["af", "be", "cd"]
CodePudding user response:
a = ["a", nil, "b", "c", nil]
b = ["d", nil, "e", "f"]
a.compact.map{ |x| [x,b.compact.pop].join }
#=> ["af", "be", "cd"]