[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array assignment with multiple args

Jeff Hales

5/14/2008 7:21:00 PM

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
--
Posted via http://www.ruby-....

4 Answers

ara.t.howard

5/14/2008 7:29:00 PM

0


On May 14, 2008, at 1:20 PM, Jeff Hales 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://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Gary Wright

5/14/2008 7:33:00 PM

0


On May 14, 2008, at 3:20 PM, Jeff Hales 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 Wright

Jeff Hales

5/14/2008 8:30:00 PM

0

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
--
Posted via http://www.ruby-....

Albert Schlef

5/14/2008 10:28:00 PM

0

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...

(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

--
Posted via http://www.ruby-....