Variable declarations on one line

a=“al”, b=“bob”, c=“carl”

Why is a an array containing al, bob, carl, and not a string “al”?

Hi –

On Mon, 2 Feb 2009, Frisco Del rosario wrote:

a=“al”, b=“bob”, c=“carl”

Why is a an array containing al, bob, carl, and not a string “al”?

It’s because of the precedence and assignment-order rules. It’s read
as:

a = x,y,z

which means that a is [x,y,z]. Meanwhile, y is this:

b = “bob”

and z is this:

c = “carl”

So you end up with a, b, and c all assigned, but not quite the way you
wanted.

Try this:

a, b, c = “al”, “bob”, “carl”

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (The Well-Grounded Rubyist)

http://www.wishsight.com => Independent, social wishlist management!

a=“al”, b=“bob”, c=“carl”

This is equivalent to:

b=“bob”
c=“carl”
a=(“al”, b, c)

What you want is:

a=“al”; b=“bob”; c=“carl”

Or rather don’t do it for stylistic reasons.

Pascal J. Bourguignon wrote:

Why did you think you could program without parentheses?

Probably because the OP’s way of initialisation is natural to a C
programmer.

Brian C. [email protected] writes:

Pascal J. Bourguignon wrote:

Why did you think you could program without parentheses?

Probably because the OP’s way of initialisation is natural to a C
programmer.

But C has its own precendence pitfalls. To be on the safe side,
always put parentheses everywhere.

But C has its own precendence pitfalls. To be on the safe side,
always put parentheses everywhere.

Now that’s funny. When I read that I had to remember some funny Ruby
code on this list with parentheses all over the place in a hilarious
“ruby has to look like lisp” way. Too sad a quick search came up with
exactly your name on that post :confused:

Even though this is absolutely the wrong place to discuss this, but
could you please stop trying to convince everybody, especially
newbies, that your way is the way to go, even though it is quite the
opposite of what should be done?

Frisco Del rosario [email protected] writes:

a=“al”, b=“bob”, c=“carl”

Why is a an array containing al, bob, carl, and not a string “al”?

(a=“al”),(b=“bob”),(c=“carl”)

is not the same as

a=(“al”,(b=“bob”),(c=“carl”))

Why did you think you could program without parentheses?

http://dept-info.labri.u-bordeaux.fr/~strandh/Teaching/Langages-Enchasses/Common/Strandh-Tutorial/syntax.html

Pascal J. Bourguignon wrote:

Brian C. [email protected] writes:

Pascal J. Bourguignon wrote:

Why did you think you could program without parentheses?

Probably because the OP’s way of initialisation is natural to a C
programmer.

But C has its own precendence pitfalls. To be on the safe side,
always put parentheses everywhere.

In my experience, using the comma operator in expressions (especially
multiple assignments) is a very standard C idiom, and the comma operator
has such low precedence that it isn’t a problem.

for (a=1, b=1; a < 100; t=b, b=a+b, a=t) { … }

You don’t like infix operators. We don’t like parentheses. I think we
should just agree to disagree.

Because a = “one”, “two”, “three” Is identical to a= [“one”, “two”,
“three”]

You want semicolons between statements, not commas

Sent from my iPhone

On 02/02/2009, at 11:28 PM, Frisco Del rosario
<[email protected]

On Tue, 3 Feb 2009, Pascal J. Bourguignon wrote:

Brian C. [email protected] writes:

Pascal J. Bourguignon wrote:

Why did you think you could program without parentheses?

Probably because the OP’s way of initialisation is natural to a C
programmer.

But C has its own precendence pitfalls. To be on the safe side,
always put parentheses everywhere.

To be even safer, avoid being doctrinaire about things like
parentheses and, instead, learn the way the tools you’re using (Ruby,
in this case) work, so that you can make informed choices. That’s what
the OP was trying to do, and it’s a very good impulse.

David


David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (The Well-Grounded Rubyist)

http://www.wishsight.com => Independent, social wishlist management!

By the way, the title of this thread prompts me to point out that there
is
no such thing as a variable declaration in Ruby, unless you consider
formal
arguments in method and block definitions to be declarations, which I
don’t.

Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale