From: Albert Schlef Date: 2008-05-15T07:27:41+09:00 Subject: Re: Array assignment with multiple args 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 -- Posted via http://www.ruby-forum.com/.