Skip to content

Instantly share code, notes, and snippets.

@tesaguri
Last active January 6, 2016 14:41
Show Gist options
  • Save tesaguri/d9532d8d5cbaa553b68a to your computer and use it in GitHub Desktop.
Save tesaguri/d9532d8d5cbaa553b68a to your computer and use it in GitHub Desktop.
Extract values from multiple arrays one by one.
# The arrays should not contain nil.
ary1 = [1, 2]
ary2 = %w(A B C)
ary3 = %i(a b)
while (elm = ary1.shift || ary2.shift || ary3.shift) do
p elm
end
#=> 1
#=> 2
#=> "A"
#=> "B"
#=> "C"
#=> :a
#=> :b
# Iterate in reverse order likewise:
ary1 = [1, 2]
ary2 = %w(A B C)
ary3 = %i(a b)
while (elm = ary3.pop || ary2.pop || ary1.pop) do
p elm
end
#=> :b
#=> :a
#=> "C"
#=> "B"
#=> "A"
#=> 2
#=> 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment