I got an error when I want to add double value values to the array:
arr = [1,2,3]
def my_mistake(arr)
result = Array.new
arr.map { |element| result << element * 2 }
end
def solution(arr)
arr.map { |element| element * 2 }
end
But back to my mistake and the definition of the map method in Ruby.
Invokes this block once for each self element. Creates a new array containing the values returned by the block.
It seems to me that the my_mistake method should return [[2], [2, 4], [2, 4, 6]], but it is not.
Can everyone explain this thing to me?
source
share