loading...

re: How To Use The Map Method In Ruby VIEW POST

FULL DISCUSSION
 

You can also use Array#map! to replace the values within the array with the return value.

arr = [1, 2, 5, 6, 9]
arr.map { |x| Math.sqrt(x) }
puts "#{arr}" #=> [1, 2, 5, 6, 9]
arr.map! { |x| Math.sqrt(x) }
puts "#{arr}" #=> [1.0, 1.4142135623730951, 2.23606797749979, 2.449489742783178, 3.0]
 

True, I probably should have mentioned that as well.

code of conduct - report abuse