struct

Int

Inherits Comparable < Comparable < Comparable < Number < AliasMethod < Comparable < Steppable < Comparable < Value < Object

Int is the base type of all integer types.

There are five signed integer types: Int8, Int16, Int32, Int64 and Int128, being able to represent numbers of 8, 16, 32, 64, and 128 bits respectively. There are five unsigned integer types: UInt8, UInt16, UInt32, UInt64 and `UInt128.

An integer literal is an optional + or - sign, followed by a sequence of digits and underscores, optionally followed by a suffix. If no suffix is present, the literal's type is Int32, or Int64 if the number doesn't fit into an Int32:

1 # Int32

1_i8   # Int8
1_i16  # Int16
1_i32  # Int32
1_i64  # Int64
1_i128 # Int128

1_u8   # UInt8
1_u16  # UInt16
1_u32  # UInt32
1_u64  # UInt64
1_u128 # UInt128

+10 # Int32
-20 # Int32

2147483648 # Int64

Literals without a suffix that are larger than Int64::MAX represent a UInt64 if the number fits, e.g. 9223372036854775808 and 0x80000000_00000000. This behavior is deprecated and will become an error in the future.

The underscore _ before the suffix is optional.

Underscores can be used to make some numbers more readable:

1_000_000 # better than 1000000

Binary numbers start with 0b:

0b1101 # == 13

Octal numbers start with 0o:

0o123 # == 83

Hexadecimal numbers start with 0x:

0xFE012D # == 16646445
0xfe012d # == 16646445

See Integer literals in the language reference.

Instance methods

multiple_of?(number)

Check whether the integer is evenly divisible by the argument.

  0.multiple_of?(0)  # => true
  6.multiple_of?(5)  # => false
  10.multiple_of?(2) # => true
Source
ordinal

Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

  1.ordinal     # => "st"
  2.ordinal     # => "nd"
  1002.ordinal  # => "nd"
  1003.ordinal  # => "rd"
  -11.ordinal   # => "th"
  -1021.ordinal # => "st"
Source
ordinalize

Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

  1.ordinalize     # => "1st"
  2.ordinalize     # => "2nd"
  1002.ordinalize  # => "1002nd"
  1003.ordinalize  # => "1003rd"
  -11.ordinalize   # => "-11th"
  -1021.ordinalize # => "-1021st"
Source