Forum: Ruby Order of Operations

541dfcb20c548ddae64fdd34487da808?d=identicon&s=25 Kevin Ja (hopscotch)
on 2014-03-24 22:06
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.
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-03-24 23:05
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 !
541dfcb20c548ddae64fdd34487da808?d=identicon&s=25 Kevin Ja (hopscotch)
on 2014-03-25 00:50
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.