ruby integerIn Ruby, as in most of programming, whole numbers are referred to as integers, while fractions (or numbers with decimal points) are referred to as floats, or floating-point numbers. An integer can be a sequence of numbers, or digits, as long as you want. 1 is an integer, as is 50, as is 9128083901230. You get the picture.

In this guide, we’ll go over what you need to know about Ruby integer objects. For more Ruby tips, check out this beginner’s Ruby programming course, or this guide that teaches you how to use the Ruby on Rails framework.

What is an Integer, Anyway?

The definition of an integer, programming aside, is simply a whole number free of fractional component. This means 10 is an integer, but 10.5, or 10 1/2 are not. In programming, integers are usually treated like primitives, which are some of the most basic operations you can find in programming.

Ruby is different, in that it treats integers as actual objects. What this means is integers, like any other object, are associated with classes. For integers, those classes are Fixnum and Bignum.

Fixnum and Bignum

They might sound funny, but fixnum and bignum are really important when we’re talking Ruby integers. In Ruby, integers are just objects, part of the larger classes fixnum and bignum. To learn more about classes and class hierarchy in programming, consider taking a class on programming fundamentals. An entry-level course like that one will teach you all the basic programming concepts you need to know to understand the information presented in this guide.

Fixnum is a class in Ruby that contains objects with immediate value, or integers that can be conveyed via machine word. Fixnum objects are immutable, which means you can’t alter them after they’ve been created. When a Fixnum object is assigned as a parameter, rather than a reference to the Fixnum object being passed, the Fixnum object itself is passed.

If a Fixnum object exceeds its range, it converts into a Bignum object. Unlike Fixnum objects, Bignum objects do not have immediate values.

Difference Between Ruby Integer and Float

As mentioned before, an integer must be a whole, real number. For fractions or  other inexact numbers, we create floating point numbers, or float objects, instead.

Get up and running with Ruby on Rails in this tutorial course on the popular Ruby framework.

Converting To and From Integers

You can convert numbers in Ruby, meaning you can convert an inexact number like a float into a whole integer, as it suits your needs.

To convert a floating point number in Ruby to a Ruby integer, you’d simply wrap the float in an integer, as shown below:

Integer (950.12)
=> 950

In the example above, Ruby simply rounded the floating number 950.12 down to 950, which is a whole integer. You can convert strings to an integer in the same way!

Integer ("587920")
=> 587920

If you’re starting with integers, you can convert these to strings or floats as well. To convert an integer into a floating point number, simply wrap it in float, like so:

Float (20)
=> 20.0

20.0 is still a whole number, literally and mathematically, but programmatically, Ruby reads it as a float, since there’s a decimal. It’s still just 20, but in Ruby, if it’s 20.0, it needs to be treated as a float.

See below for more integer conversion tips. For more on converting numbers in Ruby, and other important concepts, check out this beginner’s Ruby tutorial course.

Integer (0x13792)
=> 79762
Integer (02350)
=> 1256
Integer (0b11101010110)
=> 1878

Want to learn how to do hex and binary conversions in your head? Check out this course on mental math hex and binary conversions.

What Can You Do With An Integer?

The Ruby language comes with a huge set of operators, which are essential when working with integers. From arithmetic to assignment operators, below is a list of ones you’ll be using alongside your work with integers.

For more on Ruby operators and how to use them, you can check out this beginner’s Ruby programming course.

Arithmetic Operators in Ruby

Assignment Operators in Ruby

Comparison Operators in Ruby

Want more Ruby help? Check out this advanced Ruby programming course for a more in-depth look at the language, or get started with Ruby’s most popular framework, Ruby on Rails.

Ruby students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo