I apologize if this has been covered, but I searched and could not find
anything resembling this issue. I am fairly new to Ruby and I’m having
a strange problem that may be my misunderstanding of how Ruby works.
I am trying to declare multiple arrays on one line, like such:
array1, array2, array = Array::new()
What happens is that the arrays are all nil and [seemingly] immutable.
When I declare each array individually, like such:
What happens is that the arrays are all nil and [seemingly] immutable.
This is a part of ruby that can trip us all up at times. What you’re
seeing
here is implicit array expansion on the right-hand side. It’s easiest to
illustrate with a motivating example:
a, b, c = [1, 2, 3]
On the left-hand side, we have 3 variables, and on the right-hand side,
we
have 1 value. The value on the right is an Array, so it is expanded into
the
equivalent:
a, b, c = 1, 2, 3
If the array isn’t big enough to satisfy all the left-hand side
variables, it puts
“nil” in the expanded assignment:
Michael – thank you for the clear explanation and the online manual
reference. I sensed it was something I was doing wrong… and I was
right! Best regards, Andy.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.