order of evaluation for method arguments

Discussion in 'Ruby' started by Michael Garriss, Sep 13, 2003.

  1. Is this code guaranteed to always work this way:

    a = [1,2,3,4,5]
    --> [1, 2, 3, 4, 5]
    def m( a, b, *c )
    puts a, b, c
    end
    --> nil
    m( a.shift, a.shift, a )
    1
    2
    3
    4
    5
    --> nil

    I'm wondering (hoping) that the arguments will always be evaluated from
    left to right as is the case above. Is this a 'ruby rule' or something
    that could change?

    TIA,
    Michael
     
    Michael Garriss, Sep 13, 2003
    #1
    1. Advertising

  2. Michael Garriss

    Tim Hunter Guest

    On Sat, 13 Sep 2003 09:12:57 +0900, Michael Garriss wrote:

    > Is this code guaranteed to always work this way:
    >
    > a = [1,2,3,4,5]
    > --> [1, 2, 3, 4, 5]
    > def m( a, b, *c )
    > puts a, b, c
    > end
    > --> nil
    > m( a.shift, a.shift, a )
    > 1
    > 2
    > 3
    > 4
    > 5
    > --> nil
    >
    > I'm wondering (hoping) that the arguments will always be evaluated from
    > left to right as is the case above. Is this a 'ruby rule' or something
    > that could change?
    >
    > TIA,
    > Michael


    Maybe it's just my C background, but I've always felt like depending on
    the order of operations like this is a code smell. I'm curious about why
    you're "hoping" it works the way you've described?
     
    Tim Hunter, Sep 13, 2003
    #2
    1. Advertising

  3. Michael Garriss wrote:

    > Is this code guaranteed to always work this way:
    >
    > a = [1,2,3,4,5]
    > --> [1, 2, 3, 4, 5]
    > def m( a, b, *c )
    > puts a, b, c
    > end
    > --> nil
    > m( a.shift, a.shift, a )
    > 1
    > 2
    > 3
    > 4
    > 5
    > --> nil
    >
    > I'm wondering (hoping) that the arguments will always be evaluated
    > from left to right as is the case above. Is this a 'ruby rule' or
    > something that could change?
    >


    Let me extend the question to include arrays (which might be related to
    arguments, I don't know, I've never looked at the ruby source):

    a = [1,2,3,4,5]
    --> [1, 2, 3, 4, 5]
    b = [a.shift,a.shift,a.shift,a.shift,a.shift]
    --> [1, 2, 3, 4, 5]


    Michael
     
    Michael Garriss, Sep 13, 2003
    #3
  4. Hi,

    In message "Re: order of evaluation for method arguments"
    on 03/09/13, Michael Garriss <> writes:

    |> I'm wondering (hoping) that the arguments will always be evaluated
    |> from left to right as is the case above. Is this a 'ruby rule' or
    |> something that could change?
    |
    |Let me extend the question to include arrays (which might be related to
    |arguments, I don't know, I've never looked at the ruby source):

    It's left to right. But I don't encourage you to depend on it.

    matz.
     
    Yukihiro Matsumoto, Sep 13, 2003
    #4
  5. Michael Garriss

    daz Guest

    "Michael Garriss" <> wrote:

    > Michael Garriss wrote:
    >
    > > Is this code guaranteed to always work this way:
    > >
    > > a = [1,2,3,4,5]
    > > --> [1, 2, 3, 4, 5]
    > > def m( a, b, *c )
    > > puts a, b, c
    > > end
    > > --> nil
    > > m( a.shift, a.shift, a )
    > > 1
    > > 2
    > > 3
    > > 4
    > > 5
    > > --> nil
    > >
    > > I'm wondering (hoping) that the arguments will always be evaluated
    > > from left to right as is the case above. Is this a 'ruby rule' or
    > > something that could change?
    > >


    From Pickaxe:

    The Ruby Language
    Method Definition
    Method Arguments

    (I can't find this section on rubycentral/book to link to)


    "The expressions are evaluated from left to right.
    An expression may reference a parameter that precedes it
    in the argument list."

    #--------------------
    a = [1,2,3,4,5]

    def m( a, b, *c )
    p a, b, c
    end

    m( a.shift, b=a, a )
    #--------------------


    #-> 1
    #-> [2, 3, 4, 5]
    #-> [[2, 3, 4, 5]]


    So that's as safe as Pickaxe.

    >
    > Let me extend the question to include arrays (which might
    > be related to arguments, I don't know, I've never looked
    > at the ruby source):
    >
    > a = [1,2,3,4,5]
    > --> [1, 2, 3, 4, 5]
    > b = [a.shift,a.shift,a.shift,a.shift,a.shift]
    > --> [1, 2, 3, 4, 5]
    >


    Well, since:

    a, b = b, a

    is 'defined' (in the sense that it's a ruby idiom),
    your example works the way I'd expect, even though
    Matz has guarded his options. Perhaps he means that
    this could look like a prime candidate for optimization
    which would give [1, 1, 1, 1, 1] but I don't know if
    anyone would like to see that happen.


    >
    > Michael
    >



    daz
     
    daz, Sep 13, 2003
    #5
  6. Michael Garriss

    ts Guest

    >>>>> "d" == daz <> writes:

    d> "Michael Garriss" <> wrote:

    >> a = [1,2,3,4,5]

    --> [1, 2, 3, 4, 5]
    >> b = [a.shift,a.shift,a.shift,a.shift,a.shift]

    --> [1, 2, 3, 4, 5]
    >>


    d> Well, since:

    d> a, b = b, a

    d> is 'defined' (in the sense that it's a ruby idiom),
    d> your example works the way I'd expect, even though
    d> Matz has guarded his options.

    I don't see the relation between the two.

    The first modify `a' in the RHS, the second not.

    Perhaps, it can exist a version of ruby which give

    b = [5, 4, 3, 2, 1]

    and where

    a, b = b, a

    still work


    Guy Decoux
     
    ts, Sep 13, 2003
    #6
  7. Michael Garriss

    daz Guest

    "ts" <> wrote:

    > >>>>> "d" == daz <> writes:

    >
    > d> "Michael Garriss" <> wrote:
    >
    > >> a = [1,2,3,4,5]

    > --> [1, 2, 3, 4, 5]
    > >> b = [a.shift,a.shift,a.shift,a.shift,a.shift]

    > --> [1, 2, 3, 4, 5]
    > >>

    >
    > d> Well, since:
    >
    > d> a, b = b, a
    >
    > d> is 'defined' (in the sense that it's a ruby idiom),
    > d> your example works the way I'd expect, even though
    > d> Matz has guarded his options.
    >
    > I don't see the relation between the two.
    >
    > The first modify `a' in the RHS, the second not.
    >
    > Perhaps, it can exist a version of ruby which give
    >
    > b = [5, 4, 3, 2, 1]
    >
    > and where
    >
    > a, b = b, a
    >
    > still work
    >
    >
    > Guy Decoux
    >
    >



    I defer, of course ;)

    I was merely trying to convey that Ruby evaluates all
    RHS (in sequence) before assigning to LHS and it's
    no accident (indeed sensible) that:

    r = [1,2,3]

    a, b = r.shift, r.shift
    p [a, b]

    #-> [1, 2]



    daz
     
    daz, Sep 13, 2003
    #7
    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. Ilias Lazaridis
    Replies:
    2
    Views:
    407
    Ilias Lazaridis
    Apr 24, 2005
  2. dragoncoder

    Order of evaluation of function arguments

    dragoncoder, Dec 21, 2005, in forum: C Programming
    Replies:
    21
    Views:
    871
    Keith Thompson
    Dec 23, 2005
  3. Peter
    Replies:
    7
    Views:
    328
    Mark P
    Mar 22, 2007
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    800
    Ilias Lazaridis
    Apr 4, 2005
  5. Krishna Chaitanya

    Order of evaluation of arguments in a subroutine

    Krishna Chaitanya, Dec 9, 2010, in forum: Perl Misc
    Replies:
    1
    Views:
    87
    Keith Thompson
    Dec 9, 2010
Loading...

Share This Page