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-forum.com/.
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-forum.com/.
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
On Mon, 2 Feb 2009, Frisco Del rosario wrote:
--
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!
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?
--
__Pascal Bourguignon__
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.
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-forum.com/\.
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-forum.com/\.
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__
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?
--
Dominik Honnef
dominikho@gmx.net
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-forum.com/\.
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
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.
--
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!
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