In Ruby, Integer class is the basis for the two concrete classes that hold whole numbers. These concrete classes are Bignum and Fixnum. Fixnum holds integer values that are shown in the native machine word, whereas Bignum holds the integer value outside the range of Fixnum. Integer class contains a wide range of methods that are used for performing specified tasks. Integer class is a subclass of Numeric class.
Below is the list of methods that defined under integer class:
- to_i
- chr
- downto
- floor
- integer?
- next
- times
- upto
- ceil
- to_int
- truncate
- succ
- round
- to_i : This method returns int. The synonym of this method is to_int.
Syntax:int.to_i
- chr : This method is used to return a string that contains the ASCII character represented by the receiver’s value. The return type of this method is string.
Syntax:
int.char
Example:
# Ruby program for explaining# chr methodputs65.chrputs ?a.chrchevron_rightfilter_noneOutput:
A a
- downto : This method is used for passing decreasing values from int down to and including integer in the iterator block. The return type of this method is integer.
Syntax:
int.downto(integer){|i| block}Example:
# Ruby program for explaining# downto method6.downto(1){|i| print i,"..."}print"stop"chevron_rightfilter_noneOutput:
6...5...4...3...2...1...stop
- floor : This method retuns the largest integer less than or equal to int. This method is equivalent to to_i method. The return type of this method is integer.
Syntax:int.floor
Example:
# Ruby program for explaining# floor methodputs1.floorputs (-1).floorchevron_rightfilter_noneOutput:
1 -1
- integer? : This method always returns true for integer value and false for non-integer. The return type of this method is boolean.
Syntax:
int.integer?
Example:
# Ruby program for explaining# integer? methodputs2.integer?puts0.1.integer?chevron_rightfilter_noneOutput:
true false
- next : This method returns the integer that is equal to int+1. The return type of this method is an integer. The synonym of this method is succ.
Syntax:int.next int.succ
Example:
# Ruby program for explaining# next methodputs5.nextputs -20.nextchevron_rightfilter_noneOutput:
6 -19
- times : In this method, iterates block int times, passing in values from zero to int-1. The return type of this method is integer.
Syntax:int.times{|i| block}Example:
# Ruby program for explaining# times method6.timesdo|i|print i," "endchevron_rightfilter_noneOutput:
0 1 2 3 4 5
- upto : In this method, iterates block, passing in integer values from int up to and including receiver’s value. The return type of this method is integer.
Syntax:
int.upto(integer){|i| block}Example:
# Ruby program for explaining# upto method20.upto(25){|a| print a,"... "}chevron_rightfilter_noneOutput:
20... 21... 22... 23... 24... 25...
- round : This method is used to round off the int value. It returns floating point number when the given value is positive, self for zero and when the given value is negative, then it is rounded down.
Syntax:
int.round
Example:
# Ruby program for explaining# round methodputs2.roundputs (29.67).roundchevron_rightfilter_noneOutput:
2 30
Reference: https://ruby-doc.org/core-2.4.0/Integer.html
Recommended Posts:
- Ruby | Thread Class-Public Class Methods
- Ruby | Exception Class and its Methods
- Ruby | Range Class Methods
- Ruby | Float Class
- Ruby | Symbol Class
- Ruby | Struct Class
- Ruby | Hash Class
- Ruby | Dir Class and its methods
- Ruby | MatchData Class
- Ruby | Exception Class and its Methods
- Ruby | MatchData Class
- Ruby | Dir Class and its methods
- Ruby | Struct Class
- Ruby | Range Class Methods
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

