order of evaluation for method arguments

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default order of evaluation for method arguments

    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 Guest

  2. Similar Questions and Discussions

    1. Tab Order always greyed out, need to redefine order but can't
      I had a check box but then i had to delete it and change it to a text field. now my tab order is all out of whack and I can't seem to set the tab...
    2. Web Method Order not changing
      Hi All, I have created a Web Services with five web methods. When I browse it through the url : http://localhost/webservices/test/test.asmx it...
    3. The best overloaded method match ...' has some invalid arguments
      Dear : I have a compilation error generated by this scenario: i have : 1. class Libray 2. Web Service 3. Web Application the web...
    4. Updating records in order (into an order)
      I'm storing questions for a user-defined quiz. I can store them in the order they are entered without any problem. But... The user needs to have...
    5. Order of evaluation of expressions
      Is this: @s = qw(a b); $z = shift(@s) . shift(@s); print $z; guaranteed to print "ab"?
  3. #2

    Default Re: order of evaluation for method arguments

    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 Guest

  4. #3

    Default Re: order of evaluation for method arguments

    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 Guest

  5. #4

    Default Re: order of evaluation for method arguments

    Hi,

    In message "Re: order of evaluation for method arguments"
    on 03/09/13, Michael Garriss <mgarriss@earthlink.net> 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 Guest

  6. #5

    Default Re: order of evaluation for method arguments


    "Michael Garriss" <mgarriss@earthlink.net> 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 Guest

  7. #6

    Default Re: order of evaluation for method arguments

    >>>>> "d" == daz <dooby@d10.karoo.co.uk> writes:

    d> "Michael Garriss" <mgarriss@earthlink.net> 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 Guest

  8. #7

    Default Re: order of evaluation for method arguments


    "ts" <decoux@moulon.inra.fr> wrote:
    > >>>>> "d" == daz <dooby@d10.karoo.co.uk> writes:
    >
    > d> "Michael Garriss" <mgarriss@earthlink.net> 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139