Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > What is happening with this Unary minus?

Reply
Thread Tools

What is happening with this Unary minus?

 
 
Todd Burch
Guest
Posts: n/a
 
      08-27-2007
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/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      08-27-2007
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

 
Reply With Quote
 
 
 
 
Todd Burch
Guest
Posts: n/a
 
      08-27-2007
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/.

 
Reply With Quote
 
Jonas Roberto de Goes Filho (sysdebug)
Guest
Posts: n/a
 
      08-27-2007
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ordinary unary function to STL adaptable unary predicate how? SpOiLeR C++ 10 10-19-2005 01:18 PM
STL bind1st counterpart for unary function Fred Ma C++ 2 02-05-2004 10:52 AM
c++ unary increment operator MSDousti C++ 1 11-01-2003 07:52 PM
Unary minus applied to unsigned int Andrew Ward C++ 6 09-26-2003 03:19 PM
Unary + / - Dave Theese C++ 2 09-13-2003 02:41 PM



Advertisments
 



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