Breaking order of precedence? Ruby | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Breaking order of precedence? Ruby

Why does the code puts 4 / 2 * 2 outputs 4 instead of 1? Should'nt it multiply first then divide?

20th Mar 2019, 1:35 PM
Kalief
2 Answers
+ 2
Python or JS evaluate operations with equally ranked operators from left to right (except pow/root); I assume Ruby does the same. If you want a different behavior you'd need parentheses.
20th Mar 2019, 1:42 PM
HonFu
HonFu - avatar
+ 1
Thank you.
20th Mar 2019, 1:46 PM
Kalief