Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, some JS stuff and more...

Github:
/danielmoralesp
Twitter:
@danielmpbp

2025-02-04

Everything in Ruby is an Object

Now we have enough knowledge to continue our talkings about Ruby classes and objects. We have been studying OOP in detail and doing challenges. Let’s talk about other topics related with OOP in Ruby

Everything is an object in Ruby
As we already know, according to the Ruby paradigm, we’re dealing with an Oriented-object programming language. But at the same time all data types and data structures in Ruby are built on top of this same paradigm, this means that String data type is a class declared in some part of the Ruby language. So, we can create an object and assign it to a variable. Let’s check the following code:

➜  ~ irb
2.6.8 :001 > s = String.new("Hello world!")
 => "Hello world!" 
2.6.8 :002 > s.length
 => 12 

As you can see, we instantiate the class String with the method “.new” and assign a parameter as an initializer. And then we called the “.length” class method. Do you remember that we explained how to initialize and create class methods? Well, if you follow that same syntax you’ll understand easily what we’ve just explained. Same thing happen if we create an Array:

2.6.8 :009 > a = Array.new([1, 2, 3, 4])
 => [1, 2, 3, 4] 
2.6.8 :010 > a.length
 => 4 
2.6.8 :011 > a.first
 => 1 
2.6.8 :012 > a.last
 => 4 


We’ve created a new array object and initialize it with the values 1, 2, 3, and 4. After that we called 3 different class methods: “.length”, “.first”, “.last”. Each class method gives us a different result according to tier behavior. Same thing happens with other data types, even with Integers, Floats, Hashes and so on. 

Personally I think this is one of the greatest advantages of working with Ruby, because we don’t need to create a lot of commonly used behaviors from scratch. For instance, if we want to know the last element of an array we don’t have to reiterate over each element of the array and choose the last one, we need just to execute the built-in function called “.last”

Built-in Functions in Ruby
According to wikipedia, in computer software, in compiler theory, an intrinsic function (or built-in function) is a function (subroutine) available for use in a given programming language whose implementation is handled specially by the compiler. Typically, it may substitute a sequence of automatically generated instructions for the original function call, similar to an inline function. Unlike an inline function, the compiler has an intimate knowledge of an intrinsic function and can thus better integrate and optimize it for a given situation.

So, as you can see Ruby solves a lot of different and typical behaviors for us. There are tons of built-in functions in Ruby and it depends on the object (or data type) we want to instantiate (or create). For example let’s check the Strings built-in functions that we can use in Ruby here


On the left you can see the whole list of methods we can use with the String class. There is a huge list of methods. Most of them look to do typical things for us, for instance: capitalize, downcase, uppercase, separate strings, etc. As you can see it’s something we can use to save time and resources. 

Same thing applies to Arrays, Hashes, Integers, Floats and so on. It’s a matter of search and you can find a lot of different behavior in each data type or data structure. 

Knowing the class of an object
So far, we know how to create an object from a given class (String, Array, etc). But what if we have an object and we want to know the class? We have to reverse the process. Ruby does this easy for us. Let’s see how


2.6.8 :013 > "Hello World!".class
 => String 
2.6.8 :014 > 2563.class
 => Integer 
2.6.8 :015 > 5.69.class
 => Float 
2.6.8 :016 > false.class
 => FalseClass 
2.6.8 :017 > [1, 2, 3].class
 => Array 

With the Ruby method “.class” we can inspect the Class where the object belongs. This is very useful in some cases, like when we are returning a value from a function and we don’t know the Class. 

This is all for this post. It was short in comparison with others, but it helps to understand something very important inside the Ruby world: everything is an object!

I hope you learned a lot

Thanks for reading!
Daniel Morales