Created
May 2, 2013 13:51
-
-
Save sivabudh/5502320 to your computer and use it in GitHub Desktop.
2 different ways to transform each element in an array.
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
# initial input | |
array = ["1", "2"] | |
# did you know that this code block? | |
final_array = array.map do | elm | | |
elm.to_i | |
end | |
# does the same thing as this code block? | |
final_array = [ ] | |
array.each do |elm| | |
final_array << elm.to_i | |
end | |
# and this is the final result: transforms | |
# array of strings to array of integer | |
final_array = [ 1, 2 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment