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, and more...



Everything in Ruby is an Object

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

Everything in Ruby is an Object

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. For example, let’s check the following code:

➜  ~ irb
2.6.8 :001 > s ="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 quickly understand what we’ve just described. The same thing happens if we make an Array:

2.6.8 :009 > a =[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 initialized it with 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. The same thing happens with other data types, even with Integers, Floats, Hashes, and so on. 

Personally, I think this is one of the most significant 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. However, 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.

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

The same thing applies to Arrays, Hashes, Integers, Floats, and so on. But, again, 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 want to see 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 return a value from a function and don’t know the Class. 

This is all for this post. Of course, it was short compared to others, but it helps to understand something significant inside the Ruby world: everything is an object!

I hope you learned a lot.

Thanks for reading!
Daniel Morales