# Order of Operations

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.

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 !

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.