I'm a new learner of Ruby. I'm doing an exercise which a method will take the integers in an array, multiply by 2, then return them in a new array.
Here is my code:
array = [1,2,3]
def maps(x)
x.map { |int| int*2 }
end
p maps([array])
I get the result:
[[1, 2, 3, 1, 2, 3]]
Why is that? And how should I rewrite the code so that it will return [2,4,6]? Thanks in advance.
[[1,2,3]], an array with a single element which is an array. Ruby has a peculiar property that you can multiply an array and integer and get an array with all elements duplicated.