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 come up 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 there is a decimal point - 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 adding 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 a 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?
May 29, '24 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?
May 31, '24 14:39

Why is an empty string in Ruby not false?

meme code
meme code@memecode
Scope of a local variable in Ruby
Jun 3, '24 16:46

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.
Jun 13, '24 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 limit 4 bytes
Jun 13, '24 07:18

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

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

What is immutability and mutability?

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

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

meme code
meme code@memecode
What is a function in programming?
Jun 24, '24 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
Jun 27, '24 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?
Jun 28, '24 08:33

How to make an empty git commit?

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

Ruby library Gosu for creating 2D games

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

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

meme code
meme code@memecode