Basics:
- Ruby has two categories of numbers - integers and floating-point (also called floats).
- Integers are whole numbers that can be positive or negative but cannot be fractions.
- Depending on their size, integers can have the class
Fixnum
orBignum
. - Floats are numbers with a decimal place.
Examples:
x = 5.5
x.class
# returns
Float
x = 5
x.class
# returns
Fixnum
x = 11122233344455566677
x.class
# returns
Bignum # basically, Bignum is a very large number
# <a href='http://ruby-doc.org/core-2.0.0/Bignum.html' target='_blank' rel='nofollow'>read this article for more info</a>
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
.ceil
method rounds up to the nearest integers.
8.3.ceil
6.7.ceil
# returns:
9
7
Floor:
- The
.floor
method rounds down to the nearest integers.
8.3.floor
6.7.floor
# returns:
8
6
Next:
- Use
.next
to return the next consecutive integer.
15.next
2.next
-4.next
# returns:
16
3
-3
To String:
- The
.to_s
method changes an integer into a string.
15.to_s
# returns:
"15"
Greatest Common Denominator:
- The
.gcd
method returns the greatest common denominator of two numbers.
15.gcd(5)
9.gcd(4)
# returns:
5
1