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.chrOutput:
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"Output:
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).floorOutput:
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?Output:
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.nextOutput:
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," "endOutput:
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,"... "}Output:
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).roundOutput:
2 30
Reference: https://ruby-doc.org/core-2.4.0/Integer.html

