Skip to content

Instantly share code, notes, and snippets.

@sivabudh
Created May 2, 2013 13:51
Show Gist options
  • Save sivabudh/5502320 to your computer and use it in GitHub Desktop.
Save sivabudh/5502320 to your computer and use it in GitHub Desktop.
2 different ways to transform each element in an array.
# 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