Ruby Integers Intro
Ruby’s integers are numbers without a decimal point. So a 1, 2, 3, 4, etc is an Integer in Ruby. For comparison the Floats are 1.0, 2.0, 3.0, 4.0, 5.0, etc. Integers are used for numeric operations, providing a seamless and expressive way to work with whole numbers. Let’s embark on an exploration to grasp their fundamental characteristics.
The Ruby Integer Landscape
In Ruby, integers are the building blocks for numeric operations, representing whole numbers without any fractional component. Here’s a glimpse of how effortlessly integers can be utilized:
# Simple integer operations
x = 5
y = 10
sum = x + y # 15
difference = x - y # -5
## The sum and the difference work
## and look just like math from school.
## The plus ( + ) and minus ( - )
## symbols match what you probably saw
## in a math workbook in elementary school.
product = x * y # 50
quotient = x / y # 0???
## The product and quotient look a bit
## different if you aren't used to
## programming languages. In Ruby math
## we use ( * ) are the multiplication
## operator and we use ( / ) as the
## division operator.
## Also note that the quotient was
## 5 divided by 10 and it equalled 0.
These simple operations show off how integers look just like the normal math you learned in school and also show one example of how they are different.
Look at that quotient. Since when does 5 divided by 10 equal 0?
Well division with Integers can turn up some unexpected results if you aren’t used to them. Here are a few more examples to show what is happening.
3 / 6 # 0
4 / 3 # 1
12 / 2 # 6
13 / 2 # 6
I am going to line them up next to Floats doing the same division to show what is happening.
3 / 6 # 0
3.0 / 6.0 # 0.5
4 / 3 # 1
4.0 / 3.0 # 1.3333333333333333
12 / 2 # 6
12.0 / 2.0 # 6
13 / 2 # 6
13.0 / 2.0 # 6.5
What is going on is that Integers don’t do fractions of whole numbers. - 0.5 becomes 0
- 1.3333333333333333 becomes 1
- 6 remains 6
- 6.5 becomes 6
This shows off the difference between what Integers provide and what you might expect if this is your first encounter with them.
Integer Operations Unveiled
So Ruby integers have a plethora of operations and methods beyond the basics of addition, subtraction, multiplication, and division.
Modulo Operation
# Modulo operation returns the remainder
remainder = 17 % 5
puts "Remainder: #{remainder}"
The modulo operation offers a neat way to find remainders, a useful tool in various mathematical scenarios.
Conversion Between Floats and Integers
In Ruby you can easily convert between Integers and Floats with prebuilt Ruby methods.
# Convert integer to float
10.to_f # 10.0
33.to_f # 33.0
2.to_f # 2.0
# Convert float to integer
5.4.to_i # 5
4.3.to_i # 4
220.3.to_i # 220
Easy conversion between Floats and Integer.
- .to_f is conversion to Float
- .to_i is conversion to Integer
Exponentiation
# Exponentiation
2**3 # 8
## 2**3 is the ruby way to write
## 2 to the power of 3, also
## known as two cubed.
## That breaks down into
2 * 2 * 2 # 8
This syntax in Ruby allows easy exponentiation, enabling you to raise a number to a given power.
In conclusion, Ruby’s integers, while appearing straightforward, harbor intricacies that set them apart from normal math. Understanding the nuances of integers will make your programming easier.