Newbie question about multiple assignments

Discussion in 'Ruby' started by Alex, Jan 12, 2006.

  1. Alex

    Alex Guest

    irb(main):091:0> a=1
    => 1
    irb(main):092:0> b=2
    => 2
    irb(main):093:0> a,b=b,a
    => [2, 1]
    irb(main):094:0> a=1
    => 1
    irb(main):095:0> b=2
    => 2
    irb(main):096:0> a, b = b, a = a, b
    => [2, 1, 2] <-- Why?

    What is the order for the assignments

    --
    Posted via http://www.ruby-forum.com/.
     
    Alex, Jan 12, 2006
    #1
    1. Advertising

  2. To your why, another why: why do you need to use multiple parallel =20
    assignments in one statement? That said, parallel assignments are somewha=
    t =20
    known to be a rather messy bit of syntax I'm apparently not as savvy with=
    =20
    as I thought, since I can't make heads or tails of what is really =20
    happening.

    My personal tip woukd be that the assignments are associative =20
    right-to-left, and only the leftmost assignment is evaluated as a paralle=
    l =20
    one.

    What probably happens is broken down into simpler statements:

    a =3D a, b # a =3D=3D [1, 2]
    a, b =3D b, *a # a =3D=3D2, b=3D=3D1

    where the return value of the expression is the rvalue.

    So the end values are what you expect, just the return value of the =20
    expression isn't.

    Hint: it's easy to avoid obscure syntax, so do that if possible. Noone =20
    really admires godawful one-liners from hell in code he expects to =20
    understand a week after writing it, even if there is a certain charm to =20
    getting things done with them ;)

    Also, parallel assignment syntax is undergoing certain subtle, but =20
    significant changes that seem to resolve some of the ambiguity - you migh=
    t =20
    want to look on one of the webpages that summarize changes in Ruby 1.9.

    David Vallner

    On Thu, 12 Jan 2006 11:24:45 +0100, Alex <> wrote:

    >
    > irb(main):091:0> a=3D1
    > =3D> 1
    > irb(main):092:0> b=3D2
    > =3D> 2
    > irb(main):093:0> a,b=3Db,a
    > =3D> [2, 1]
    > irb(main):094:0> a=3D1
    > =3D> 1
    > irb(main):095:0> b=3D2
    > =3D> 2
    > irb(main):096:0> a, b =3D b, a =3D a, b
    > =3D> [2, 1, 2] <-- Why?
    >
    > What is the order for the assignments
    >
     
    David Vallner, Jan 12, 2006
    #2
    1. Advertising

  3. Alex

    Michael Ulm Guest

    Alex wrote:
    > irb(main):091:0> a=1
    > => 1
    > irb(main):092:0> b=2
    > => 2
    > irb(main):093:0> a,b=b,a
    > => [2, 1]
    > irb(main):094:0> a=1
    > => 1
    > irb(main):095:0> b=2
    > => 2
    > irb(main):096:0> a, b = b, a = a, b
    > => [2, 1, 2] <-- Why?
    >
    > What is the order for the assignments
    >


    Keep in mind, that

    a, b = c, d

    is just shorthand for

    a, b = [c, d]

    So, your expression

    a, b = b, a = a, b

    gets parsed as

    a, b = [b, a = a, b]

    and thus produces the observed behaviour.

    HTH,

    Michael


    --
    Michael Ulm
    R&D Team
    ISIS Information Systems Austria
    tel: +43 2236 27551-219, fax: +43 2236 21081
    e-mail:
    Visit our Website: www.isis-papyrus.com
     
    Michael Ulm, Jan 12, 2006
    #3
  4. On Thu, 12 Jan 2006 13:39:00 +0100, Michael Ulm =20
    <> wrote:

    > Alex wrote:
    >> irb(main):091:0> a=3D1
    >> =3D> 1
    >> irb(main):092:0> b=3D2
    >> =3D> 2
    >> irb(main):093:0> a,b=3Db,a
    >> =3D> [2, 1]
    >> irb(main):094:0> a=3D1
    >> =3D> 1
    >> irb(main):095:0> b=3D2
    >> =3D> 2
    >> irb(main):096:0> a, b =3D b, a =3D a, b
    >> =3D> [2, 1, 2] <-- Why?
    >> What is the order for the assignments
    >>

    >
    > Keep in mind, that
    >
    > a, b =3D c, d
    >
    > is just shorthand for
    >
    > a, b =3D [c, d]
    >
    > So, your expression
    >
    > a, b =3D b, a =3D a, b
    >
    > gets parsed as
    >
    > a, b =3D [b, a =3D a, b]
    >
    > and thus produces the observed behaviour.
    >
    > HTH,
    >
    > Michael
    >
    >



    That doesn't seem to explain the return value [2, 1, 2] of the expression=
     
    David Vallner, Jan 12, 2006
    #4
  5. Alex

    Karl Allmark Guest

    >> Alex wrote:
    >>> irb(main):091:0> a=1
    >>> => 1
    >>> irb(main):092:0> b=2
    >>> => 2
    >>> irb(main):093:0> a,b=b,a
    >>> => [2, 1]
    >>> irb(main):094:0> a=1
    >>> => 1
    >>> irb(main):095:0> b=2
    >>> => 2
    >>> irb(main):096:0> a, b = b, a = a, b
    >>> => [2, 1, 2] <-- Why?
    >>> What is the order for the assignments
    >>>

    >>
    >> Keep in mind, that
    >>
    >> a, b = c, d
    >>
    >> is just shorthand for
    >>
    >> a, b = [c, d]
    >>
    >> So, your expression
    >>
    >> a, b = b, a = a, b
    >>
    >> gets parsed as
    >>
    >> a, b = [b, a = a, b]
    >>
    >> and thus produces the observed behaviour.
    >>
    >> HTH,
    >>
    >> Michael
    >>
    >>

    >
    >
    >That doesn't seem to explain the return value [2, 1, 2] of the expression.
    >
    >David Vallner


    Hi List

    Just getting up to speed with ruby myself, try reading Davids explaination
    as:

    a, b = [b,(a = a), b]

    Karl Allmark
     
    Karl Allmark, Jan 12, 2006
    #5
  6. On Thu, 12 Jan 2006 14:06:56 +0100, Karl Allmark =20
    <> wrote:

    >
    > Hi List
    >
    > Just getting up to speed with ruby myself, try reading Davids =20
    > explaination
    > as:
    >
    > a, b =3D [b,(a =3D a), b]
    >
    > Karl Allmark
    >
    >
    >


    Not quite, what I had in mind would be this one-liner:

    a, b =3D [b, *[a =3D [a, b]]]

    I have a slight completely unfounded doubt the interpreter would =20
    implicitly associate (a =3D a) in the middle of the expression.

    The results observed in irb ARE however ambiguous with respect to both of=
    =20
    those possible interpretations.

    David Vallner
     
    David Vallner, Jan 12, 2006
    #6
  7. Alex

    Karl Allmark Guest


    > -----Original Message-----
    > From: Karl Allmark [mailto:]
    > Sent: 12 January 2006 14:07
    > To: ruby-talk ML
    > Subject: Re: Newbie question about multiple assignments
    >
    > >> Alex wrote:
    > >>> irb(main):091:0> a=1
    > >>> => 1
    > >>> irb(main):092:0> b=2
    > >>> => 2
    > >>> irb(main):093:0> a,b=b,a
    > >>> => [2, 1]
    > >>> irb(main):094:0> a=1
    > >>> => 1
    > >>> irb(main):095:0> b=2
    > >>> => 2
    > >>> irb(main):096:0> a, b = b, a = a, b
    > >>> => [2, 1, 2] <-- Why?
    > >>> What is the order for the assignments
    > >>>
    > >>
    > >> Keep in mind, that
    > >>
    > >> a, b = c, d
    > >>
    > >> is just shorthand for
    > >>
    > >> a, b = [c, d]
    > >>
    > >> So, your expression
    > >>
    > >> a, b = b, a = a, b
    > >>
    > >> gets parsed as
    > >>
    > >> a, b = [b, a = a, b]
    > >>
    > >> and thus produces the observed behaviour.
    > >>
    > >> HTH,
    > >>
    > >> Michael
    > >>
    > >>

    > >
    > >
    > >That doesn't seem to explain the return value [2, 1, 2] of

    > the expression.
    > >
    > >David Vallner

    >
    > Hi List
    >
    > Just getting up to speed with ruby myself, try reading Davids
    > explaination
    > as:
    >
    > a, b = [b,(a = a), b]
    >
    > Karl Allmark
    >


    Sorry I meant Micheal's explaination :-(
     
    Karl Allmark, Jan 12, 2006
    #7
  8. Alex

    Michael Ulm Guest

    David Vallner wrote:

    > On Thu, 12 Jan 2006 13:39:00 +0100, Michael Ulm
    > <> wrote:
    >
    >> Alex wrote:
    >>
    >>> irb(main):091:0> a=1
    >>> => 1
    >>> irb(main):092:0> b=2
    >>> => 2
    >>> irb(main):093:0> a,b=b,a
    >>> => [2, 1]
    >>> irb(main):094:0> a=1
    >>> => 1
    >>> irb(main):095:0> b=2
    >>> => 2
    >>> irb(main):096:0> a, b = b, a = a, b
    >>> => [2, 1, 2] <-- Why?
    >>> What is the order for the assignments
    >>>

    >>
    >> Keep in mind, that
    >>
    >> a, b = c, d
    >>
    >> is just shorthand for
    >>
    >> a, b = [c, d]
    >>
    >> So, your expression
    >>
    >> a, b = b, a = a, b
    >>
    >> gets parsed as
    >>
    >> a, b = [b, a = a, b]
    >>
    >> and thus produces the observed behaviour.
    >>

    >
    >
    > That doesn't seem to explain the return value [2, 1, 2] of the expression.
    >


    Why not? the return value of an assignment is the right hand
    side. In this case [2, 1 , 2]. The first and last value there
    coming from b, and the middle value is the return value of the
    assignment a = a.

    Try this:

    irb(main):001:0> a, b = [1, 2, 3, 4] # sets a=1 and b=2
    => [1, 2, 3, 4]

    irb(main):002:0> a, b = [1, c = 2, 3] # sets a=1, b=2, and c = 2
    => [1, 2, 3]

    HTH,

    Michael








    --
    Michael Ulm
    R&D Team
    ISIS Information Systems Austria
    tel: +43 2236 27551-219, fax: +43 2236 21081
    e-mail:
    Visit our Website: www.isis-papyrus.com
     
    Michael Ulm, Jan 12, 2006
    #8
  9. Alex

    Guest

    Hi --

    On Thu, 12 Jan 2006, David Vallner wrote:

    > On Thu, 12 Jan 2006 13:39:00 +0100, Michael Ulm
    > <> wrote:
    >
    >> Alex wrote:
    >>> irb(main):091:0> a=1
    >>> => 1
    >>> irb(main):092:0> b=2
    >>> => 2
    >>> irb(main):093:0> a,b=b,a
    >>> => [2, 1]
    >>> irb(main):094:0> a=1
    >>> => 1
    >>> irb(main):095:0> b=2
    >>> => 2
    >>> irb(main):096:0> a, b = b, a = a, b
    >>> => [2, 1, 2] <-- Why?
    >>> What is the order for the assignments
    >>>

    >>
    >> Keep in mind, that
    >>
    >> a, b = c, d
    >>
    >> is just shorthand for
    >>
    >> a, b = [c, d]
    >>
    >> So, your expression
    >>
    >> a, b = b, a = a, b
    >>
    >> gets parsed as
    >>
    >> a, b = [b, a = a, b]
    >>
    >> and thus produces the observed behaviour.
    >>
    >> HTH,
    >>
    >> Michael
    >>
    >>

    >
    >
    > That doesn't seem to explain the return value [2, 1, 2] of the expression.


    An assignment expression returns its right-hand side. In this case,
    that's [b, a = a, b]:

    a = 1
    b = 2
    [b, a = a, b] => [2,1,2]


    David

    --
    David A. Black


    "Ruby for Rails", from Manning Publications, coming April 2006!
    http://www.manning.com/books/black
     
    , Jan 12, 2006
    #9
  10. On Thu, 12 Jan 2006 15:09:01 +0100, <> wrote:

    >
    > An assignment expression returns its right-hand side. In this case,
    > that's [b, a =3D a, b]:
    >
    > a =3D 1
    > b =3D 2
    > [b, a =3D a, b] =3D> [2,1,2]
    >
    >
    > David
    >



    Ah well, for some strange reason I thought the parser would check for a =20
    parallel assignment form inside the array constructor as well. Apparently=
    =20
    not... Right, that example pretty much clears the issue up.

    David Vallner
     
    David Vallner, Jan 12, 2006
    #10
  11. David Vallner wrote:
    > On Thu, 12 Jan 2006 15:09:01 +0100, <> wrote:
    >
    >>
    >> An assignment expression returns its right-hand side. In this case,
    >> that's [b, a = a, b]:
    >>
    >> a = 1
    >> b = 2
    >> [b, a = a, b] => [2,1,2]
    >>

    >
    >
    > Ah well, for some strange reason I thought the parser would check for a
    > parallel assignment form inside the array constructor as well.
    > Apparently not... Right, that example pretty much clears the issue up.
    >

    Within method arguments, the "," gets parsed as an argument separator
    not an array separator:

    irb(main):094:0> puts( a,b = 1,2 )
    1
    1
    2
    => nil
    irb(main):095:0> puts( ( a,b = 1,2 ) )
    1
    2
    => nil



    irb(main):103:0> a,b=1,2
    => [1, 2]
    irb(main):104:0> a,b=(b,a=a,b)
    => [1, 2]
    irb(main):105:0> a
    => 1
    irb(main):106:0> b
    => 2
     
    Andrew McGuinness, Jan 12, 2006
    #11
  12. Alex

    Guest

    Hi --

    On Fri, 13 Jan 2006, David Vallner wrote:

    > On Thu, 12 Jan 2006 15:09:01 +0100, <> wrote:
    >
    >>
    >> An assignment expression returns its right-hand side. In this case,
    >> that's [b, a = a, b]:
    >>
    >> a = 1
    >> b = 2
    >> [b, a = a, b] => [2,1,2]
    >>
    >>
    >> David
    >>

    >
    >
    > Ah well, for some strange reason I thought the parser would check for a
    > parallel assignment form inside the array constructor as well. Apparently
    > not... Right, that example pretty much clears the issue up.


    The thing is, if you've got an array [a,b], and you add to it so that
    it is [a,b,c=1], the meaning of the "a,b" part doesn't change; rather,
    you're understood to have added an element. The comma semantics are
    different in these different contexts, in other words.


    David

    --
    David A. Black


    "Ruby for Rails", from Manning Publications, coming April 2006!
    http://www.manning.com/books/black
     
    , Jan 13, 2006
    #12
    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. Replies:
    3
    Views:
    389
  2. mamu
    Replies:
    4
    Views:
    624
    Barry
    Dec 11, 2008
  3. John O'Hagan
    Replies:
    3
    Views:
    263
    Luis M. González
    Dec 20, 2008
  4. Abhinav

    multiple assignments with a HoHoA

    Abhinav, Sep 17, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    106
    Gunnar Hjalmarsson
    Sep 17, 2004
  5. Chris Angelico
    Replies:
    29
    Views:
    123
    Chris Angelico
    Apr 1, 2014
Loading...

Share This Page