Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Surprising(?) behaviour of the comma operator (http://www.velocityreviews.com/forums/t812464-surprising-behaviour-of-the-comma-operator.html)

Frank Schmitt 12-08-2003 09:28 AM

Surprising(?) behaviour of the comma operator
 

Hi!

I just stumbled over a (at least for me) surprising behaviour
of the comma (',') operator:

irb(main):001:0> a,b = 1,2
[1, 2]
irb(main):002:0> puts a.class, b.class
Fixnum
Fixnum
nil

Ok, no surprises so far. Here we go:

irb(main):003:0> a=1, b=2
[1, 2]
irb(main):004:0> puts a.class, b.class
Array
Fixnum
nil

that is, a=1, b=2 creates an *Array* a instead of a Fixnum.
I don't think this is a bug, since others surely have spotted
this before me, but what's the rationale behind this?

kind regards
frank
--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com



nobu.nokada@softhome.net 12-08-2003 09:38 AM

Re: Surprising(?) behaviour of the comma operator
 
Hi,

At Mon, 8 Dec 2003 18:32:03 +0900,
Frank Schmitt wrote:
> I just stumbled over a (at least for me) surprising behaviour
> of the comma (',') operator:


Ruby has no comma operator like C.

> irb(main):003:0> a=1, b=2
> [1, 2]
> irb(main):004:0> puts a.class, b.class
> Array
> Fixnum
> nil


If you really want to use multi-statement, use semicolon (;)
instead.

--
Nobu Nakada



Hal Fulton 12-08-2003 08:05 PM

Re: Surprising(?) behaviour of the comma operator
 
nobu.nokada@softhome.net wrote:
> Hi,
>
> At Mon, 8 Dec 2003 18:32:03 +0900,
> Frank Schmitt wrote:
>
>>I just stumbled over a (at least for me) surprising behaviour
>>of the comma (',') operator:

>
>
> Ruby has no comma operator like C.
>
>
>> irb(main):003:0> a=1, b=2
>> [1, 2]
>> irb(main):004:0> puts a.class, b.class
>> Array
>> Fixnum
>> nil

>
>
> If you really want to use multi-statement, use semicolon (;)
> instead.
>


Correct of course. To clarify a little for the original poster,
the statement is equivalent to:

a = [1, b=2]



Hal




Frank Schmitt 12-09-2003 09:43 AM

Re: Surprising(?) behaviour of the comma operator
 
nobu.nokada@softhome.net writes:

> Hi,
>
> At Mon, 8 Dec 2003 18:32:03 +0900,
> Frank Schmitt wrote:
> > I just stumbled over a (at least for me) surprising behaviour
> > of the comma (',') operator:

>
> Ruby has no comma operator like C.
>
> > irb(main):003:0> a=1, b=2
> > [1, 2]
> > irb(main):004:0> puts a.class, b.class
> > Array
> > Fixnum
> > nil

>
> If you really want to use multi-statement, use semicolon (;)
> instead.


Normally I don't use multi-statements, but (coming from C++) I
wrote

def initialize(a,b)
@a = a, @b = b
end

Writing

def initialize(a,b)
@a,@b = a,b
end

instead is no big deal, I just have to remember it :-)

Thanks to all for the quick responses & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com


All times are GMT. The time now is 04:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.