a = [1,2,3,4]
b = a << 5
a == [1,2,3,4] # returns false
How to assign b
to a
with 5
appended to the end without modifying a
itself?
a = [1,2,3,4]
b = a << 5
a == [1,2,3,4] # returns false
How to assign b
to a
with 5
appended to the end without modifying a
itself?
Just sum two arrays:
a = [1,2,3,4]
b = a + [5]
# b == [1, 2, 3, 4, 5]
# a == [1, 2, 3, 4]
Ruby variables hold references to objects and the =
operator copies the references.
It seems you wish to clone
a
:
irb(main):001:0> a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):002:0> b = a.clone << 5
=> [1, 2, 3, 4, 5]
irb(main):003:0> a
=> [1, 2, 3, 4]
irb(main):004:0> b
=> [1, 2, 3, 4, 5]