Skip to content

Instantly share code, notes, and snippets.

@ukitazume
Created February 10, 2012 09:40
Show Gist options
  • Save ukitazume/1788064 to your computer and use it in GitHub Desktop.
Save ukitazume/1788064 to your computer and use it in GitHub Desktop.
# data
label = %w{aa, bb, cc}
v1 = [1, 2, 3]
v2 = [11, 22, 33]
# use zip
label.zip(v1) #= > [["aa,", 1], ["bb,", 2], ["cc", 3]]
label.zip(v1, v2) #= > [["aa,", 1, 11], ["bb,", 2, 22], ["cc", 3, 33]]
Hash[label.zip(v1)] #= > {"cc"=>3, "bb,"=>2, "aa,"=>1}
Hash[label.zip(v1.zip(v2))] #= > {"cc"=>[3, 33], "bb,"=>[2, 22], "aa,"=>[1, 11]}
# use transpose and zip
Hash[[label, v1].transpose] #= > {"cc"=>3, "bb,"=>2, "aa,"=>1}
Hash[[label, v1.zip(v2)].transpose] #= > {"cc"=>[3, 33], "bb,"=>[2, 22], "aa,"=>[1, 11]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment