Skip to content

Instantly share code, notes, and snippets.

@lenary
Forked from baxter/gist:984701
Created May 21, 2011 18:18
Show Gist options
  • Save lenary/984744 to your computer and use it in GitHub Desktop.
Save lenary/984744 to your computer and use it in GitHub Desktop.
each_with_object
# 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