Surprising(?) behaviour of the comma operator

Discussion in 'Ruby' started by Frank Schmitt, Dec 8, 2003.

  1. 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
     
    Frank Schmitt, Dec 8, 2003
    #1
    1. Advertising

  2. Frank Schmitt

    Guest

    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
     
    , Dec 8, 2003
    #2
    1. Advertising

  3. Frank Schmitt

    Hal Fulton Guest

    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
     
    Hal Fulton, Dec 8, 2003
    #3
  4. 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
     
    Frank Schmitt, Dec 9, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    509
    Barry Schwarz
    Feb 8, 2005
  2. karthik
    Replies:
    3
    Views:
    342
    karthik
    Jun 20, 2006
  3. Joe P. Cool
    Replies:
    7
    Views:
    297
    Benjamin
    Jun 29, 2008
  4. candide
    Replies:
    15
    Views:
    546
    Stephen Hansen
    Jun 18, 2010
  5. Michele Simionato

    surprising behaviour of global dictionaries

    Michele Simionato, Oct 9, 2012, in forum: Python
    Replies:
    6
    Views:
    196
    Peter Otten
    Oct 9, 2012
Loading...

Share This Page