Forum: Ruby Variable declarations on one line

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Frisco D. (Guest)
on 2009-02-02 14:29
a="al", b="bob", c="carl"

Why is a an array containing al, bob, carl, and not a string "al"?
David A. Black (Guest)
on 2009-02-02 14:35
(Received via mailing list)
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 (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
Tom L. (Guest)
on 2009-02-02 15:55
(Received via mailing list)
> 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 (Guest)
on 2009-02-02 16:14
(Received via mailing list)
Frisco Del rosario <removed_email_address@domain.invalid> 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/Teac...
Brian C. (Guest)
on 2009-02-02 18:46
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.
Pascal J. Bourguignon (Guest)
on 2009-02-02 19:00
(Received via mailing list)
Brian C. <removed_email_address@domain.invalid> 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.
Dominik H. (Guest)
on 2009-02-02 19:20
(Received via mailing list)
> 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 :/

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?
Brian C. (Guest)
on 2009-02-02 23:02
Pascal J. Bourguignon wrote:
> Brian C. <removed_email_address@domain.invalid> 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.
David A. Black (Guest)
on 2009-02-02 23:37
(Received via mailing list)
On Tue, 3 Feb 2009, Pascal J. Bourguignon wrote:

> Brian C. <removed_email_address@domain.invalid> 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 (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
Julian L. (Guest)
on 2009-02-03 04:43
(Received via mailing list)
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
<removed_email_address@domain.invalid
Rick D. (Guest)
on 2009-02-03 15:54
(Received via mailing list)
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
This topic is locked and can not be replied to.