# What is happening with this Unary minus?

**URL:** <https://www.ruby-forum.com/t/what-is-happening-with-this-unary-minus/113097>\
**Category:** Ruby\
**Created:** [August 27, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/what-is-happening-with-this-unary-minus/113097 "2007-08-27T17:06:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [August 27, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/what-is-happening-with-this-unary-minus/113097/1 "2007-08-27T17:06:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [August 27, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/what-is-happening-with-this-unary-minus/113097/2 "2007-08-27T17:38:49Z")

</div>

Stefano C. wrote:

> Alle lunedÃ¬ 27 agosto 2007, Todd B. 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

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [August 27, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/what-is-happening-with-this-unary-minus/113097/3 "2007-08-27T17:28:48Z")

</div>

Alle lunedÃ¬ 27 agosto 2007, Todd B. ha scritto:

> 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

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [August 27, 2007, 11:23pm UTC](https://www.ruby-forum.com/t/what-is-happening-with-this-unary-minus/113097/4 "2007-08-27T23:23:33Z")

</div>

Stefano C. wrote:

> > 
> 
> precedence. The first expression is interpreted as
> 
> 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
