Math and Logic


Collapse Content

Basic arithmetic

You can use the Ruby shell (IRB) as a calculator. Ruby uses the standard arithmetic operators:

1 + 1  #=> 2
10 * 2 #=> 20
35 / 5 #=> 7
10.0 / 4.0 #=> 2.5

The modulus operator returns the remainder from division:

4 % 3 #=> 1  

** is the exponent operator:

2 ** 5   #=> 32

Equality and Comparisons

Ruby lets you use standard syntax or english for equalities and logical operators.

#check equality
1 == 1 #=> true    

# Inequality
2 != 1 #=> true

# not
! true  #=> false
not true  #=> false

Logical Operators

And and Or

true && true #=> true
false || true #=> true   

you can also use words:

true and false #=> false
true or false #=> true

Everything in ruby is considered true except for false and nil:

!nil   #=> true
!false #=> true
!1     #=> false
!0     #=> false

An or statement will return the first true value it encounters:

nil || 7          #=> 7
nil || false || 8 #=> 8
1 || 2            #=> 1

This tutorial is based in part on LearnXinYminutes and is licensed under the Creative Commons Attribution-ShareAlike 3.0 License.

Challenge

You will be given two numbers a and b as input. a is considered great if a2 is greater than 4 * b and a is greater than b. Return true if a is great and false otherwise.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Contact Us
Sign in or email us at [email protected]