Ruby | Integer Class

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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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

    Syntax:

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

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    6...5...4...3...2...1...stop
  4. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:


    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    2
    30
    

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.