Skip to content

Instantly share code, notes, and snippets.

@jeanbaptistebeck
Last active February 10, 2016 10:32
Show Gist options
  • Save jeanbaptistebeck/09774fb9d88c80124915 to your computer and use it in GitHub Desktop.
Save jeanbaptistebeck/09774fb9d88c80124915 to your computer and use it in GitHub Desktop.
Ruby's Zip Method

Ruby's Zip Method

[1,2,3].zip(['a', 'b', 'c'])
#=> [[1, "a"], [2, "b"], [3, "c"]]
['a', 'b', 'c'].zip( [1,2,3], ['oogie', 'boogie', 'booger'] )
#=> [["a", 1, "oogie"], ["b", 2, "boogie"], ["c", 3, "booger"]]

Size matters

['FIRST-1', 'FIRST-2'].zip( ['SECOND-1','SECOND-2','SECOND-3'], ['THIRD-1'] )
#=> [  ["FIRST-1", "SECOND-1", "THIRD-1"], 
       ["FIRST-2", "SECOND-2", nil]   ]
['FIRST-1', 'FIRST-2', 'FIRST-3'].zip( ['SECOND-1'], ['THIRD-1', 'THIRD-2'] )
#=> [  ["FIRST-1", "SECOND-1", "THIRD-1"],
       ["FIRST-2", nil, "THIRD-2"],
       ["FIRST-3", nil, nil]  ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment