Variable declarations on one line

  • Thread starter Frisco Del rosario
  • Start date
F

Frisco Del rosario

a="al", b="bob", c="carl"

Why is a an array containing al, bob, carl, and not a string "al"?
 
D

David A. Black

Hi --

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 (http://manning.com/black2)

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

Tom Link

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

Brian Candler

Pascal said:
Why did you think you could program without parentheses?

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

Pascal J. Bourguignon

Brian Candler said:
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.
 
D

David A. Black

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 (http://manning.com/black2)

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

Julian Leviston

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]
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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.
 

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

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top