Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Surprising(?) behaviour of the comma operator

Reply
Thread Tools

Surprising(?) behaviour of the comma operator

 
 
Frank Schmitt
Guest
Posts: n/a
 
      12-08-2003

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


 
Reply With Quote
 
 
 
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      12-08-2003
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


 
Reply With Quote
 
 
 
 
Hal Fulton
Guest
Posts: n/a
 
      12-08-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) 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



 
Reply With Quote
 
Frank Schmitt
Guest
Posts: n/a
 
      12-09-2003
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the behaviour of * operator an example of operator oveloading in c? karthik C Programming 3 06-20-2006 02:58 PM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM
Confused about the comma operator Derek C++ 6 04-13-2004 09:03 PM
Yet another comma operator question Koster C Programming 7 01-18-2004 01:07 AM
Overloading comma to give a concatenation operator: various problems Paul Davis C++ 7 07-17-2003 07:25 AM



Advertisments