Ruby Numbers Methods - thelastmile/FreeCodeCamp GitHub Wiki
Ruby Numbers Methods
In Ruby there are a variety of build in methods you can perform on numbers.
Even:
- Use
.even?to check whether or not an integer is even.
15.even?
4.even?
# returns:
false
true
Odd:
- Use
.odd?to check whether or not an integer is odd.
15.odd?
4.odd?
# returns:
true
false
Ceil:
- The
.ceilmethod rounds up to the nearest integers.
8.3.ceil
6.7.ceil
# returns:
9
7
Floor:
- The
.floormethod rounds down to the nearest integers.
8.3.floor
6.7.floor
# returns:
8
6
Next:
- Use
.nextto return the next consecutive integer.
15.next
2.next
-4.next
# returns:
16
3
-3
To String:
- The
.to_smethod changes an integer into a string.
15.to_s
# returns:
"15"
Greatest Common Denominator:
- The
.gcdmethod returns the greatest common denominator of two numbers.
15.gcd(5)
9.gcd(4)
# returns:
5
1
| Home | Next |
|---|---|
| Ruby Numbers Basics | Ruby Numbers Operations |