[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Variable declarations on one line

Frisco Del rosario

2/2/2009 12:29:00 PM

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

Why is a an array containing al, bob, carl, and not a string "al"?
--
Posted via http://www.ruby-....

9 Answers

David A. Black

2/2/2009 12:34:00 PM

0

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

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

pjb

2/2/2009 1:34:00 PM

0

Frisco Del rosario <friscodelrosario@sbcglobal.net> 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/s...

--
__Pascal Bourguignon__

ThoML

2/2/2009 1:55:00 PM

0

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

Brian Candler

2/2/2009 4:46:00 PM

0

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

pjb

2/2/2009 4:54:00 PM

0

Brian Candler <b.candler@pobox.com> 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.

--
__Pascal Bourguignon__

Brian Candler

2/2/2009 9:02:00 PM

0

Pascal J. Bourguignon wrote:
> Brian Candler <b.candler@pobox.com> 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.
--
Posted via http://www.ruby-....

David A. Black

2/2/2009 9:37:00 PM

0

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

> Brian Candler <b.candler@pobox.com> 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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

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

Julian Leviston

2/3/2009 2:36:00 AM

0

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 <friscodelrosario@sbcglobal.net
> wrote:

> a="al", b="bob", c="carl"
>
> Why is a an array containing al, bob, carl, and not a string "al"?
> --
> Posted via http://www.ruby-....
>

Rick DeNatale

2/3/2009 1:53:00 PM

0

[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.
--
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...