# Order of Operations

**URL:** <https://www.ruby-forum.com/t/order-of-operations/240016>\
**Category:** Ruby\
**Created:** [March 24, 2014, 10:06pm UTC](https://www.ruby-forum.com/t/order-of-operations/240016 "2014-03-24T22:06:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [March 24, 2014, 10:06pm UTC](https://www.ruby-forum.com/t/order-of-operations/240016/1 "2014-03-24T22:06:59Z")

</div>

Sorry if this is too noob for this forum, but I’m literally just getting  
started with learning how to code. I’m having a hard time figuring out  
the order of operations for Ruby.

25 + 30 / 6 = 30

I totally get this. It takes 25 + (30/6).

100 - 25 \* 3 % 4 = 97

I get this too. It’s basically taking 100 minus 4% of 75.

But I can’t for the life of me get this:

3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 = 7

Can someone please explain this one to me? I understand that because the  
code isn’t using a floating decimal it’s going to be off. But I can’t  
even get close to figuring out how the above comes to 7.

This seems so simple that I should be able to figure it out. Maybe  
coding is not for me? Would really appreciate some help. Thanks.

---

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [March 24, 2014, 11:05pm UTC](https://www.ruby-forum.com/t/order-of-operations/240016/2 "2014-03-24T23:05:17Z")

</div>

Kevin Ja wrote in post #1140807:

with irb :

irb(main):008:0\> 4 % 2  
=\> 0  
irb(main):009:0\> 1 / 4  
=\> 0  
irb(main):010:0\> 3+2+1-5+6  
=\> 7

> I understand that because the code isn’t using a floating decimal it’s  
> going to be off.

that’s right !

Each time you have a doubt with ruby, open an irb session and test !

---

<div class="post-metadata">

**Author:** ![hopscotch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@hopscotch](https://www.ruby-forum.com/u/hopscotch)\
**Post date:** [March 25, 2014, 12:50am UTC](https://www.ruby-forum.com/t/order-of-operations/240016/3 "2014-03-25T00:50:19Z")

</div>

Regis d’Aubarede wrote in post #1140816:

> Each time you have a doubt with ruby, open an irb session and test !

Thanks Regis! You also just showed me the irb, which I didn’t even know  
existed or how to use it.

I’m now a little worried I’ll never be able to wrap my head around the  
way Ruby does math. So it’s basically doing: 1 + 0 + 0 +6 = 7. With 1  
being (3+2+1-5), 0 being (4 % 2), another 0 being (1 / 4), and the 6  
being (6).

Seems very strange to me. Although, I have zero programming experience,  
I can follow operations if brackets are used (like in Excel). I’d like  
to see this as: (3+2+1-5) + (4%2) - (1 / 4) + 6 = 7. And even here, the  
4%2 seems backwards. But I guess there’s a reason why it needs to be  
this way for coding. I just have to learn why. Thanks again.
