Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | Integer Class

  • Last Updated : 27 Sep, 2021

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

1. to_i : This method returns int. The synonym of this method is to_int

Syntax: 

int.to_i

2. 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




# Ruby program for explaining
# chr method
 
puts 65.chr        
puts ?a.chr       

Output: 

A
a

3. 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 an integer.

Syntax:

int.downto(integer){|i| block}

Example: 

Ruby




# Ruby program for explaining
# downto method
 
6.downto(1){|i| print i, "..."
print "stop" 

Output: 

6...5...4...3...2...1...stop

4. floor : This method returns 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




# Ruby program for explaining
# floor method
 
puts 1.floor     
puts (-1).floor   

Output: 

1
-1

5. 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




# Ruby program for explaining
# integer? method
 
puts 2.integer?    
puts 0.1.integer? 

Output:

true
false

6. 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




# Ruby program for explaining
# next method
 
puts 5.next     
puts -20.next   

Output: 

6
-19

7. 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




# Ruby program for explaining
# times method
 
6.times do|i| 
print i, " "
end

Output:

0 1 2 3 4 5

8. 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




# Ruby program for explaining
# upto method
 
20.upto(25){|a| print a, "... "}

Output: 

20... 21... 22... 23... 24... 25...

9. 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




# Ruby program for explaining
# round method
 
puts 2.round     
puts (29.67).round

Output: 

2
30

Reference: https://ruby-doc.org/core-2.4.0/Integer.html
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!