-
-
Save lenary/984744 to your computer and use it in GitHub Desktop.
each_with_object
This file contains 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
# Why does the first one work but not the second one? | |
> (1..10).each_with_object([]) {|i, a| a << i*2; puts a.object_id } | |
2151926500 | |
2151926500 | |
2151926500 | |
2151926500 | |
2151926500 | |
2151926500 | |
2151926500 | |
2151926500 | |
2151926500 | |
2151926500 | |
# => [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] | |
> (1..10).each_with_object([]) {|i, a| a += [i*2]; puts a.object_id } | |
2151867640 | |
2151867340 | |
2151867140 | |
2151866960 | |
2151865900 | |
2151865700 | |
2151865640 | |
2151865540 | |
2151865440 | |
2151865360 | |
# => [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment