Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Ruby Classes for Numbers
3.
Arithmetic in Ruby
4.
Number Methods in Ruby
5.
Understanding with Example
5.1.
Output
6.
Frequently Asked Questions
6.1.
What is “number.integer?” in Ruby?
6.2.
How do you add numbers together in Ruby?
6.3.
What are Ruby objects?
6.4.
What is Fixnum in Ruby?
6.5.
What kind of language is Ruby?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Numbers in Ruby

Author Kanak Rana
0 upvote

Introduction

To understand a programming language, you must first understand what types of data it can manipulate and what it can do with that data. This blog discusses the values that ”Numbers in Ruby” provides.

Numbers in Ruby

Numbers in Ruby support two types of numbers:

  • Integers
  • Floating-point numbers
     

Integers: An integer is simply a series of digits, such as 19, 180, etc. Integers are numbers that do not contain any decimal points. In Ruby, integers are objects of the class  Fixnum(32 or 64-bit) or Bignum classes (used for bigger numbers).

Floating-point numbers: Numbers with decimals are known as float values such as 3.1, 5.9, etc.

Ruby Classes for Numbers

Ruby has five built-in classes for representing numbers, and the standard library has three more numeric classes that can be useful at times. This diagram depicts the class hierarchy.

Class Hierarchy

In numbers in Ruby, all number objects are numeric instances. All integers are integer instances. 

Fixnum is an integer value that fits within 31 bits (on most implementations). It is otherwise a Bignum. Bignum objects represent integers of any size, and if the result of a Fixnum operation is too large to fit in a Fixnum, it is transparently converted to a Bignum. 

Similarly, if the result of a Bignum operation falls within the range of Fixnum, the result is a Fixnum. The Float class in Ruby approximates real numbers by using the platform's native floating-point representation.

Arithmetic in Ruby

Standard "+, -, *,  /, %, and **" operators for addition, subtraction, multiplication, division, modulus, and exponential are defined respectively for all numeric types in Ruby. When an integer result is too large for a Fixnum, Ruby converts it to a Bignum automatically, and as a result, integer arithmetic in Ruby never overflows, as it does in many other languages.

# Adding two integer values
puts 5 + 3

# Adding one integer and one float value
puts 4 + 3.0

# Subtracting two integer values
puts 5 - 2

# Multiplication of two integer values
puts 7 * 3

# Division of two integer values
puts 10 / 2

# Demonstration of Exponential operation
puts 5 ** 3

#Modulus − Divides left-hand operand by right hand operand and returns remainder.
puts 20%10
You can also try this code with Online Ruby Compiler
Run Code

Output

8
7.0
3
21
5
125
0


Following is the table of a different class of numbers in Ruby:

Class

Description

Example

Integer

Parent class for Fixnum & Bignum 1, 3, 4

Float

Decimal Numbers

1.0, 4.3,5.1

 

Fixnum

Whole numbers that fit into the integer type  12134, 5266

Bignum

Bigger numbers 111111111111111111111

Big Decimal

Precision decimal numbers

1.124234

 

Complex 

Includes imaginary numbers (1+0i), (5+0i)

Rational

Fractions 5/9, 7/2

The ‘Complex’, ‘BigDecimal’, and ‘Rational’ classes are not built into Ruby but are included in the standard library. The ‘Complex’ class represents complex numbers. ‘BigDecimal’ uses a decimal representation rather than a binary representation to represent real numbers with arbitrary precision. The ‘Rational’ represents rational numbers by dividing one integer by another.

Number Methods in Ruby

There are several methods of numbers in ruby like- Even, Odd, Ceil, Floor, Next, Pred, To String, GCD(Greatest common denominator), etc.

  • Even: Using ".even?" to check whether an integer is even or not.
     
  • Odd: Using “.odd?” to check whether the integer is odd or not and returns a true or false boolean.
     
  • Ceil: The “.ceil” method rounds the floats up to the nearest number and returns an integer.
     
  • Floor: The “.floor” method rounds the floats down to the nearest number and returns an integer.
     
  • Next: Use “.next” to get the next integer in the sequence.
     
  • Pred: The “.pred” function returns the previous consecutive integer.
     
  • To String: Using ".to s"  on a number (integer, float, etc.) returns the number as a string.
     
  • Greatest Common Denominator: The greatest common divisor (always positive) of two numbers is calculated using the “.gcd” method.
     
  • Round: Use “.round" to return a rounded integer or float.
     
  • Times: Use “.times” to iterate the given block ‘int’ times, passing in values ranging from 0 to int - 1.

Understanding with Example

An example showing mathematical methods and functions of numbers in Ruby:

# Even
puts 19.even?
puts 6.even?  

# Odd
puts 19.odd?
puts 8.odd?

# Ceil
puts 9.4.ceil
puts 4.7.ceil

# Floor
puts 6.3.floor
puts 8.9.floor

# Next
puts 13.next
puts 9.next  
puts -6.next

# Pred
puts 12.pred
puts 5.pred  
puts (-7).pred

# To String
puts 13.to_s  
puts 12.4.to_s

#GCD
puts 13.gcd(5)
puts 9.gcd(-2)

#round
puts 6.round        
puts 6.round(2)    
puts 15.round(-1)  

#times
6.times do |i|
   print i, " "
 end
You can also try this code with Online Ruby Compiler
Run Code

Output

Output of the arithmetic code

Frequently Asked Questions

What is “number.integer?” in Ruby?

The “number.integer?” is a method that checks if the number taken is an integer or not.

How do you add numbers together in Ruby?

The ‘+’ is an inbuilt method in Ruby that returns the addition of two numbers. It returns ‘a + b’ where ‘a’ and ‘b’ numbers.

What are Ruby objects?

In Ruby, everything is an object. All objects have an identity; they can also hold state and respond to messages to manifest behavior. Normally, these messages are delivered via method calls. The string is an example of a Ruby object.

What is Fixnum in Ruby?

A Fixnum has integer values that can be represented in the native machine word (minus 1 bit).

What kind of language is Ruby?

Ruby is an object-oriented language.

Conclusion

This blog covered the numbers in Ruby. The ruby classes of numbers, arithmetic in ruby, and method in ruby were also covered, and for knowledge of ruby, visit, 

If you would like to learn more, check out these articles on the following to have a better understanding:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Thank you

Happy Learning Ninja! 🥷

Live masterclass