Array assignment with multiple args

J

Jeff Hales

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 Hales
 
A

ara.t.howard

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/
 
G

Gary Wright

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 Wright
 
J

Jeff Hales

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) :p
 
A

Albert Schlef

Jeffrey said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top