3

I want to assign an empty array to multiple variables. Here is what I'm doing:

irb(main):015:0> a, b, c = []
=> []
irb(main):016:0> a
=> nil
irb(main):017:0> b
=> nil
irb(main):018:0> c
=> nil

It gives me nil. I wonder why? But if I did this:

irb(main):019:0> a, b, c = [], [], []
=> [[], [], []]
irb(main):020:0> a
=> []
irb(main):021:0> b
=> []
irb(main):022:0> c
=> []

then it works as I expect, but it's a little bit longer than the first one. What's wrong with the first example?

2
  • Nothing is wrong with the first one. Ruby is responding correctly. What is it that you claim that is wrong? Commented Jan 20, 2013 at 9:08
  • Do you want to multiply the values of the variables (which you surely cannot do with arrays), or do you want multiple variables? Commented Jan 20, 2013 at 9:09

4 Answers 4

2

I believe this example will help you understand the problem:

[1] pry(main)> a, b, c = [1,2]
=> [1, 2]
[2] pry(main)> a
=> 1
[3] pry(main)> b
=> 2
[4] pry(main)> c
=> nil

Now back to your problem, you are trying to assign the elements in an empty array to variables, as a result, the three variables all get nil value.

Sign up to request clarification or add additional context in comments.

Comments

1
a = b = c = []

but note that all variables will be assigned the same array
so:

a = b = []
b << 1
p b        # [1]
p a        # [1]

Comments

1

Parallel Assignment With Single Rvalue

If the assignment contains multiple lvalues and one rvalue, the Ruby attempts to expand the rvalue into an array as if you'd called #to_a on the rvalue. So, the problem with your example of a, b, c = [] is that it is semantically equivalent to:

a, b, c = nil.to_a

which would obviously assign nil to the first variable, and leave the others unassigned (which is also nil). In contrast, consider this:

a, b, c = 1
a
# => 1
b
# => nil
c
# => nil

The same principle is at work, but now you can see that the first lvalue does receive an assignment from the right-hand side; it just wasn't obvious when the assignment was nil.

3 Comments

if so, why a = nil.to_a #=> []; irb(main):028:0> a # => []. Why a is equal to [] instead of nil like in my example?
which would obviously assign nil to the first variable, and leave the others unassigned (which is also nil). In contrast, consider this: but "a" gives me nil and not []
@AlanDert It's not assigning an empty array; it's assigning the first value of the empty array.
0

Ruby destructures arrays when you do parallel assignment:

a, b, c = [:foo, :bar, :baz]
a # => :foo
b # => :bar
c # => :baz

If you give it an array with too few entries, it sets the remaining values to nil:

a, b, c = [:foo, :bar]
a # => :foo
b # => :bar
c # => nil

So your first example is just the natural extension of this - an empty array definitely has too few entries to assign any of the variables!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.