Skip to content

Instantly share code, notes, and snippets.

@chris-ramon
Created February 4, 2014 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-ramon/8812905 to your computer and use it in GitHub Desktop.
Save chris-ramon/8812905 to your computer and use it in GitHub Desktop.
each, map, select - ruby
# each
h = [10, 20, 30]
hh = h.each {|e| e*2 } # does not create new object
# print h
# print h.object_id == hh.object_id # true
# map
x = [1, 2, 3]
z = x.map { |e| e*2 } # does create new object - non-destructive manner
# print x
# print x.object_id == z.object_id # false
# print z
foo = x.map! { |e| e*2 } # does not create new object - destructive manner
# print foo.object_id == x.object_id # true
# select
bar = [12, 15, 29, 19]
baz = bar.select {|e| e > 18 }
print bar #[12, 15, 29, 19]
print baz #[29, 19]
print bar.object_id == baz.object_id # false
# collect
# same as map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment