All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What will be the result of adding 10.5 and 10?

Post cover: What will be the result of adding 10.5 and 10?
This content has been automatically translated from Ukrainian.
A seemingly simple question can arise in an interview for a junior ruby dev position. What will be the result of adding 10.5 and 10? What's the catch?
10.5 + 10
Let's break down the example. What is the difference between 10.5 and 10? Notice the .5, that is, the floating point. Let's check the classes of these two objects.
10.5.class
=> Float
10.class
=> Integer
And here lies the catch. We are adding objects of two classes. What will be the result if we add Float and Integer?
(10.5 + 10).class
=> Float
So after adding Float and Integer, we will get Float. What does this mean? It means that when a decimal point is present, the result will have a decimal point.
10.5 + 10
=> 20.5
To prove this, we can write tests. We will also check if we get an object of class Integer when we add 10 + 10.
Let's create a file operations_spec.rb:
require 'rspec'

RSpec.describe 'Addition operations' do
  context 'when adding Float and Integer' do
    it 'returns Float' do
      result = 10.5 + 10
      expect(result).to be_a(Float)
      expect(result).to eq(20.5)
    end
  end

  context 'when adding Integer and Integer' do
    it 'returns Integer' do
      result = 10 + 10
      expect(result).to be_a(Integer)
      expect(result).to eq(20)
    end
  end
end
We run it and get the result that confirms everything we wrote above.
rspec operations_spec.rb
..

Finished in 0.01617 seconds (files took 0.31407 seconds to load)
2 examples, 0 failures
Everything is quite simple. But you need to know these nuances.

This post doesn't have any additions from the author yet.

What is the difference between nil and false in Ruby?
29 May 20:59

What is the difference between nil and false in Ruby?

meme code
meme code@memecode
Why is an empty string in Ruby not false?
31 May 14:39

Why is an empty string in Ruby not false?

meme code
meme code@memecode
The scope of a local variable in Ruby
03 Jun 16:46

The scope of a local variable in Ruby

meme code
meme code@memecode
What is the difference between int and bigint in Ruby? Minimum and maximum values.
13 Jun 06:37

What is the difference between int and bigint in Ruby? Minimum and maximum values.

meme code
meme code@memecode
What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes
13 Jun 07:18

What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes

meme code
meme code@memecode
What is immutability and mutability?
19 Jun 07:48

What is immutability and mutability?

meme code
meme code@memecode
[Ruby] What is the difference between variables that start with @, @@, and $?
23 Jun 14:00

[Ruby] What is the difference between variables that start with @, @@, and $?

meme code
meme code@memecode
What is a function in programming?
24 Jun 18:15

What is a function in programming?

meme code
meme code@memecode
[Fix] extconf.rb failed during the installation of the Ruby library Gosu
27 Jun 16:38

[Fix] extconf.rb failed during the installation of the Ruby library Gosu

meme code
meme code@memecode
How to make an empty git commit?
28 Jun 08:33

How to make an empty git commit?

meme code
meme code@memecode
Ruby library Gosu for creating 2D games
29 Jun 08:48

Ruby library Gosu for creating 2D games

meme code
meme code@memecode
Gosu Ruby Tutorial - пройдемось по офіційній документації
03 Jul 11:50

Gosu Ruby Tutorial - пройдемось по офіційній документації

meme code
meme code@memecode