[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What is happening with this Unary minus?

Todd Burch

8/27/2007 3:06:00 PM

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-....

3 Answers

Stefano Crocco

8/27/2007 3:27:00 PM

0

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

Todd Burch

8/27/2007 3:39:00 PM

0

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-....

Jonas Roberto de Goes Filho (sysdebug)

8/27/2007 9:23:00 PM

0

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://g...