Array assignment with multiple args

Hi all. I’m just starting to learn Ruby and have hit a problem that is
being extremely difficult to find information on. When given a line of
code such as:

a = “this”, b = “that”, c = “those”

Is there a concise and elegant way of describing why variable a is
assigned as an array, but variable b is not? There must be a simple
explanation as to ‘why’ this is happening. The only description of what
is going on here that I have found is that this is interpreted as

a = “this”, (b = “that”), (c = “those”)

and not

a = “this”, (b = “that”, c = “those”)

and that the arguments are read from right to left. This hasn’t really
helped my understanding, as that much can be deduced just by putting the
line through the Ruby interpreter, and I don’t see how reading the
assignments from right to left assigns [“this”,“that”,“those”] to
variable a but only “that” to variable b.

Thank you for taking the time to ponder this.
Jeff H.

On May 14, 2008, at 1:20 PM, Jeff H. wrote:

The only description of what
is going on here that I have found is that this is interpreted as

a = “this”, (b = “that”), (c = “those”)

and not

a = “this”, (b = “that”, c = “those”)

and that the arguments are read from right to left.

start reading left to right, say ‘a equals everything to the right of
the equal sign’, now interpret the right side… continue

a @ http://codeforpeople.com/

On May 14, 2008, at 3:20 PM, Jeff H. wrote:

Hi all. I’m just starting to learn Ruby and have hit a problem that is
being extremely difficult to find information on. When given a line of
code such as:

a = “this”, b = “that”, c = “those”

This statement is parsed as:

a = [ “this”, (b = “that”), (c = “those”) ]

The right hand side of the assignment to variable ‘a’
is an array of three items:
first item: a string with value “this”
second item: a string with value “that”
third item: a string with value “those”

As a side effect, the variable b is assigned to “that”
and the variable c is assigned to “those”

The example you gave is pretty unusual. I think you
were thinking about parallel assignment, which would
look like:

a, b, c = “this”, “that”, “those”

Gary W.

I know about parallel assignment, but the oddity of this statement’s
behavior has had me stumped. I also know to interpret this statement as
an assignee, assignor and assignment. I would like to be able to explain
‘why’ the other assignments are allowed; and, since they are, ‘why’ b is
not assigned [“that”,“those”]. I know this must sound like a 3 year
old’s question (eg: but, why? ad infinitum) :stuck_out_tongue:

Jeffrey H. wrote:

When given a line of code such as:

a = “this”, b = “that”, c = “those”

[…]
have found is that this is interpreted as

a = “this”, (b = “that”), (c = “those”)

and not

a = “this”, (b = “that”, c = “those”)

I would like to be able to explain ‘why’ the other assignments are allowed

The code of your program is parsed, by a “parser”. The parser has rules
about what can be found in a program, and if you look at these rules
you’ll discover why your code doesn’t tranlstae to ’ a = “this”, (b =
“that”, c = “those”) '.

The rules can be found as BNF notation:

http://docs.huihoo.com/ruby/ruby-man-1.4/yacc.html

(that doc it’s probably quite obsolete, but it can serve our purpose.)

You can see that multiple assignment is described using this pattern:

MLHS `=’ MRHS

It stands for “Multiple-assiggment Left Hand Side = Multiple-assignment
Right Hand Side”.

Now, this expression is part of a larger structure: EXPR (it stand
probbaly for “expression”).

Now, let’s look at the pattern for MRHS:

ARGS [,'*’ ARG]

Now, let’s look at the pattern for ARGS:

ARG (`,’ ARG)*

It’s composed of ARG thingies, so let’s look at the pattern for ARG:

ARG : LHS =' ARG | LHS OP_ASGN ARG | ARG…’ ARG
| ARG ...' ARG | ARG+’ ARG
| ARG -' ARG | ARG*’ ARG
[lot’s of lines snipped]

Now comes the important part: you’ll discover that there’s a thing ARG
can’t be: EXPR. And since the MLHS=MRHS is under EXPR, it follows that
the right hand side or multiple assignment can’t (directly) contain a
multiple asignment.

In other words, ’ a = “this”, b = “that”, c = “those” ’ can’t be parsed
into ’ a = “this”, (b = “that”, c = “those”) ’ because this isn’t a
valid structure.

The BNF I used my not be the actual one used for ruby, but that’s not
important, I just wanted to show why a parser chooses one interpretation
for a source-code and not another.

==

BTW, you can see to what tree Ruby parses your source by installing the
‘rubynode’ gem. Then you’d be able to do:

require ‘rubygems’
require ‘rubynode’
pp ’ a = “this”, b = “that”, c = “those” '.parse_to_nodes.transform

Hi,

In message “Re: Array assignment with multiple args”
on Thu, 15 May 2008 04:20:35 +0900, Jeff H. [email protected]
writes:

|Hi all. I’m just starting to learn Ruby and have hit a problem that is
|being extremely difficult to find information on. When given a line of
|code such as:
|
|a = “this”, b = “that”, c = “those”
|
|Is there a concise and elegant way of describing why variable a is
|assigned as an array, but variable b is not? There must be a simple
|explanation as to ‘why’ this is happening. The only description of what
|is going on here that I have found is that this is interpreted as
|
|a = “this”, (b = “that”), (c = “those”)

More precisely,

a = (“this”, (b = “that”), (c = “those”))

But I am not sure what you wanted to accomplish by

a = “this”, b = “that”, c = “those”

In Ruby, commas are not expression separator.

|and not
|
|a = “this”, (b = “that”, c = “those”)
|
|and that the arguments are read from right to left. This hasn’t really
|helped my understanding, as that much can be deduced just by putting the
|line through the Ruby interpreter, and I don’t see how reading the
|assignments from right to left assigns [“this”,“that”,“those”] to
|variable a but only “that” to variable b.

Multiple assignment does not nest. Assignments in right hand side is
considered as normal assignments.

          matz.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs