add element to ruby array return new array

Solution 1:

You can easily add two arrays in Ruby with plus operator. So, just make an array out of your element.

arr = [1, 2]
puts arr + [3]
# => [1, 2, 3]
puts arr
# => [1, 2]

Solution 2:

it also works by extending arr using * operator

arr = [1,2]
puts [*arr, 3]
=> [1, 2, 3]