What is happening with this Unary minus?

Discussion in 'Ruby' started by Todd Burch, Aug 27, 2007.

  1. Todd Burch

    Todd Burch Guest

    LOOPER = 6

    -(LOOPER).upto(LOOPER) {|i|
    puts i }

    I get one line of output: 6

    However, I get 13 lines of output here:

    (-LOOPER).upto(LOOPER) {|i|
    puts i }

    What is happening with the unary minus on the first example? I expected
    to get identical output.

    Todd
    --
    Posted via http://www.ruby-forum.com/.
     
    Todd Burch, Aug 27, 2007
    #1
    1. Advertising

  2. Alle luned=C3=AC 27 agosto 2007, Todd Burch ha scritto:
    > LOOPER =3D 6
    >
    > -(LOOPER).upto(LOOPER) {|i|
    > puts i }
    >
    > I get one line of output: 6
    >
    > However, I get 13 lines of output here:
    >
    > (-LOOPER).upto(LOOPER) {|i|
    > puts i }
    >
    > What is happening with the unary minus on the first example? I expected
    > to get identical output.
    >
    > Todd


    I'm not completely sure, but I think the difference arises because of opera=
    tor=20
    precedence. The first expression is interpreted as

    =2D(LOOPER.upto(LOOPER){|i| puts i})

    Since the lower and upper bounds are equal, the iteration is performed only=
    =20
    one time. The - is then applied to the return value of upto (the receiver,=
    =20
    i.e LOOPER). Indeed, if you try your code in irb, you'll see that the value=
    =20
    of the expression is -6.

    In the second case, using brackets you tell the interpreter that the upto=20
    method should not be called on LOOPER, but on (-LOOPER), that is on -6.

    I hope this helps

    Stefano
     
    Stefano Crocco, Aug 27, 2007
    #2
    1. Advertising

  3. Todd Burch

    Todd Burch Guest

    Stefano Crocco wrote:
    > Alle lunedì 27 agosto 2007, Todd Burch ha scritto:
    >
    > I'm not completely sure, but I think the difference arises because of
    > operator
    > precedence. The first expression is interpreted as
    >
    > Stefano


    Hi Stefano. I see that now. I did this:

    result = -(LOOPER).....
    puts result

    and I see the minus applied now. Thanks!

    Todd
    --
    Posted via http://www.ruby-forum.com/.
     
    Todd Burch, Aug 27, 2007
    #3
  4. Stefano Crocco wrote:
    > Alle lunedì 27 agosto 2007, Todd Burch ha scritto:
    >
    >> LOOPER = 6
    >>
    >> -(LOOPER).upto(LOOPER) {|i|
    >> puts i }
    >>
    >> I get one line of output: 6
    >>
    >> However, I get 13 lines of output here:
    >>
    >> (-LOOPER).upto(LOOPER) {|i|
    >> puts i }
    >>
    >> What is happening with the unary minus on the first example? I expected
    >> to get identical output.
    >>
    >> Todd
    >>

    >
    > I'm not completely sure, but I think the difference arises because of operator
    > precedence. The first expression is interpreted as
    >
    > -(LOOPER.upto(LOOPER){|i| puts i})
    >
    > Since the lower and upper bounds are equal, the iteration is performed only
    > one time. The - is then applied to the return value of upto (the receiver,
    > i.e LOOPER). Indeed, if you try your code in irb, you'll see that the value
    > of the expression is -6.
    >
    > In the second case, using brackets you tell the interpreter that the upto
    > method should not be called on LOOPER, but on (-LOOPER), that is on -6.
    >
    > I hope this helps
    >
    > Stefano
    >
    >
    >

    This is correct. On the first example, the loop is
    (LOOPER).upto(LOOPER) {|i| puts i } added post minus operator

    --
    Jonas Roberto de Goes Filho (sysdebug)
    http://goes.eti.br
     
    Jonas Roberto de Goes Filho (sysdebug), Aug 27, 2007
    #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. Andrew Ward
    Replies:
    6
    Views:
    687
    Ron Natalie
    Sep 26, 2003
  2. Marc

    Semantics of unary minus

    Marc, Feb 12, 2006, in forum: C Programming
    Replies:
    13
    Views:
    497
  3. Matthew  Cook
    Replies:
    6
    Views:
    505
  4. Sam Stephenson
    Replies:
    9
    Views:
    232
    Caleb Clausen
    Nov 7, 2005
  5. sreservoir

    unary minus strangeness

    sreservoir, Mar 28, 2010, in forum: Perl Misc
    Replies:
    7
    Views:
    118
    Ilya Zakharevich
    Apr 1, 2010
Loading...

Share This Page