Last active
January 6, 2016 14:41
-
-
Save tesaguri/d9532d8d5cbaa553b68a to your computer and use it in GitHub Desktop.
Extract values from multiple arrays one by one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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