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 = String.new("Hello world!")
=> "Hello world!"
2.6.8 :002 > s.length
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 = Array.new([1, 2, 3, 4])
=> [1, 2, 3, 4]
2.6.8 :010 > a.length
2.6.8 :011 > a.first
2.6.8 :012 > a.last
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
2.6.8 :014 > 2563.class
2.6.8 :015 > 5.69.class
2.6.8 :016 > false.class
2.6.8 :017 > [1, 2, 3].class
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!