DEV Community

loading...

Discussion on: How To Use The Map Method In Ruby

Collapse
drbragg profile image
Drew Bragg

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]
Collapse
seanolad profile image
Sean Author

True, I probably should have mentioned that as well.