Ruby Numbers Methods - ashish9342/FreeCodeCamp GitHub Wiki
In Ruby there are a variety of build in methods you can perform on numbers.
- Use
.even?
to check whether or not an integer is even.
15.even?
4.even?
# returns:
false
true
- Use
.odd?
to check whether or not an integer is odd.
15.odd?
4.odd?
# returns:
true
false
- The
.ceil
method rounds up to the nearest integers.
8.3.ceil
6.7.ceil
# returns:
9
7
- The
.floor
method rounds down to the nearest integers.
8.3.floor
6.7.floor
# returns:
8
6
- Use
.next
to return the next consecutive integer.
15.next
2.next
-4.next
# returns:
16
3
-3
- The
.to_s
method changes an integer into a string.
15.to_s
# returns:
"15"
- The
.gcd
method 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 |