Created
February 4, 2014 21:40
-
-
Save chris-ramon/8812905 to your computer and use it in GitHub Desktop.
each, map, select - ruby
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
# 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