Nikita Kazakov
Nikita Kazakov
3 min read

Tags

If you open IRB (interactive ruby) and type in the following expression 2 + 3, you’ll get 5. If you reverse that with 3 + 2, you’ll still get 5.

Numbers are just one of the many types of objects in Ruby. Whole numbers are an Integer type of object.

2.class     # Integer
4.class     # Integer
32.class    # Integer
2392.class  # Integer

So what is really happening when you type in 2+3?

</figure>

2 + 3
# Returns 5. But it can be rewritten like this
# to see explicitly how +(3) is actually a message sent
# to the 2 object. 
2.+(3)

A +(3) message is sent to the object 2. The outer circle shows that the Integer object knows how to handle the -, +, and = message.

The 2 object receives the + 3 message. + is the message and 3 is the object passed as the parameter with that message. After the message is received, the 2 object returns a new object 5.

That’s what is meant by everything is an object. You’re sending messages to objects and you’re receiving objects.

Ruby is an expressive language and uses what’s called syntactic sugar to make certain things appear cleaner to programmers.

2.+(3) #Without syntactic sugar.
2 + 3 # With Ruby's syntactic sugar. 

So what happens when you do 3 + 2? You’re sending a +2 message to the 3 object and it returns a 5 object.

Same idea with any math operations.

Notice that Integer objects are immutable–they don’t change. When you take 3 and send a +2 message to it, 3 doesn’t change. Instead, it returns a new Integer object 5.

Not all objects are immutable. If we look at a cash register object and send a message deposit(50), the cash value of the object will change. The cash register object is mutable.

</figure>

Stacking Messages

You can also stack messages together on one line.

What happens when you 20 + 10 - 5?

20 object receives +(10) message. It returns a 30 object. The 30 object receives a -(5) message and returns a 25 object.

The object is always on the left and the message is followed by a . and a message.

Here’s how it would look like without Ruby syntactic sugar:

20 + 10 - 5    # 25 (With syntactic sugar)
20.+(10).-(5)  # 25 (What's actually happening without syntactic sugar)

Custom adding method

To drive the point home, let’s create another way to add numbers in Ruby. Instead of doing 2 + 3, we’ll add a new method to the Integer class called add.

It’s frowned upon to monkey patch an existing object with custom methods.

We open up the Integer class and write a add method. It will do the same thing as + method does.

class Integer
  def add(num)
    self + num
  end
end

It takes itself and adds the given parameter to itself.

2.add(3)
# 5

Even the + method itself can be modified to give strange results. We can modify the + to multiply the two numbers instead.

class Integer
  def +(num)
    self * num
  end
end

2 + 6  # 12
3 + 5  # 15

This is why Ruby is flexible. Nothing is sacred and all objects can be dissected and modified. In practice however, you won’t see developers doing this to standard Ruby objects.

Strings are objects

Strings are also objects in Ruby.

puts "hello world"
# outputs hello world on the screen

"hello world" is a string object in Ruby.

"hello world".class
#String

Ruby applies syntactic sugar when you type in "hello world". What’s really happening in the background is the string object is created like this String.new("hello world").

"hello world" is then sent as a message to puts which displays the string on your screen.

Why objects?

It’s because we have an intuitive sense of what objects are in the world. An apple is an object. It has properties like color or a type of apple. You can do something to it (bite it) and it can do something to you (act on your taste buds).

Object oriented programming sets up a framework for describing how pieces of an application talk to each other. It creates a familiar structure of understanding.