Simple syntax question

In:

=== code start ==== a=10, b=20 c=100, d=2200 #x=>1000, y=>2000 # MultipleExpressionsPerLine.rb:3:

p a, b p c, d === code end ===

Ruby puts out four lines (under SciTE):

[10, 20] 20 [100, 2200] 2200

Q1: Why does Ruby somehow create an array for lines 1 & 2? Q2: Why does Ruby assign this array to a, but the scalar (20) to b? Q3: If Ruby's willing to build arrays for lines 1 & 2, why won't it build a hash for line 3?

I'm running WinXP-Pro/SP2, Ruby 1.8.2-15, Rails 1.1.6.

Hi --

In:

=== code start ==== a=10, b=20 c=100, d=2200 #x=>1000, y=>2000 # MultipleExpressionsPerLine.rb:3:

p a, b p c, d === code end ===

Ruby puts out four lines (under SciTE):

[10, 20] 20 [100, 2200] 2200

Q1: Why does Ruby somehow create an array for lines 1 & 2? Q2: Why does Ruby assign this array to a, but the scalar (20) to b? Q3: If Ruby's willing to build arrays for lines 1 & 2, why won't it build a hash for line 3?

I'm running WinXP-Pro/SP2, Ruby 1.8.2-15, Rails 1.1.6.

When you do this:

   a=10, b=20

the way it's parsed is:

   a = 10, (b=20)

The assignment of 20 to b happens first, and then the whole rhs is assigned to a. From a's point of view, this is the same as:

   a = 10,20

and assigning a literal list like that works like assigning an array.

As for the third line: x => 1000 isn't a legal expression on its own. => is not an assignment operator; it's just a hash key/value separator.

David

Hi David,

Thanks for your clarification. Now that you explain (a=10, b=20), my brain said: You read that before, Dummy ... why do you always forget things?

I had just found a nice tutorial on sorting tables by clicking on column headings. It included a long line of comma-separated assignments and I couldn't make sense of it. Now I'm grounded again!

Again, thanks.

Best wishes, Richard

dblack@wobblini.net wrote: