Methods of Ruby’s Integer Class
Learn about the methods that are available in Ruby for the integer class.
We'll cover the following
There are not too many integer class methods, and it’s worth looking at the documentation to better understand what’s available for a programmer. In the upcoming sections, we’ll take a close look at some of them.
even?
and odd?
even?
or odd?
are two methods of the integer class whose method names have a question mark at the end by default.
We can use any of these two methods to check if an integer is even or odd. Since the question mark at the end of the method has come up for the first time in this course, let’s see why it’s placed there.
The question mark indicates that the method returns type boolean. Technically, there is no boolean in Ruby. We just introduced it for the purpose of this course. There are two types that we will see in Ruby: TrueClass and FalseClass. This means that there are true and false keywords in the Ruby language, and we can use these keywords like they have been used in the examples below.
Something unambiguous in real life can be represented by true or false. Usually, these methods start with the prefix is
—for example, lightswitch.is_on?
.
There are no other options for using boolean; it’s either true or false. The question mark is optional in Ruby, but the programming community expects programmers to use it nonetheless. It’s recommended to have a ?
at the end of methods that return a boolean value.
Let’s look at an example:
Get hands-on with 1200+ tech skills courses.