Ruby Language for Beginners Part 4 - Ruby Objects

Ruby Language for Beginners Part 4 - Ruby Objects

Hello!

This is a post from the Ruby Language for Beginners in 8 Parts!

In the today's post we're going to see Ruby Classes and Objects!

Ruby Objects

Everything in Ruby is an Object! Even a simple number is an object. Just to remember, an object holds a piece of some information in attributes and this information can be used to execute actions, in methods.

With that in mind we can call methods like this:

As you can see, a String is an object also and we can call a lot of great Ruby methods!

Let's reverse the name for example:

And the output will be: edoC gnikcaH

We could just create an Array (let's see about Arrays in a second!) with the words in a sentence just by using the split method:

And the tutorials variable will have the following Array:

["Ruby", "Python", "Java", "Go", "Erlang"]

But where does the Ruby objects come from? It comes from Ruby Classes!

Ruby Classes

Every object in Ruby is created from a Class. A Class is kind of a skeleton for an object, defining methods and variables that an object can use. You can think a Class is an abstraction that describes how we can interact with an object. Objects are created from a Class.

1 - Let's print the the Class for a few objects:

Before creating our own Ruby classes, let's see a few built-in classes that are written in the Ruby Language.

In the method below we created a method that, given a value, prints its Ruby Class:

The output will be:

String Fixnum Float Array

2 - What about creating our own class in Ruby?

We can create our own classes in Ruby in order to have a better cohesion and separation of concerns. You'll work a lot with classes in Ruby : )

To create an object of the MySpecialClass we need to instantiate it.

We instantiated the object using the keyword new in the Class. If you execute the code:

I'm special method inside a special Ruby Classes and Methods!

In another Post we're going to really deep dive into Ruby Classes!

Instance Variables in Ruby Objects

Instance variables are variables that can be access outside the method by another method in the same class.

Imagine the class that we already created, called MySpecialClass:

Now we would like to keep that special message public by another method. We can do that by using a special character "@" before the variable.

In the Ruby code below, we're going to create a instance variable for the class called @message

And you can ask: But how does that attribute is seem outside the method? Let's going to create another method to test it:

If you execute the code above, you'll have the following result:

I'm special method inside a special Ruby class! I have access to the variable I'm special method inside a special Ruby class!

As you can see, we have access to the instance variable outside the method because the attribute belongs to the class and not just to the method.

But what happens if you remove the "@" character?

The method another_method will not have access to the variable and the output will be:

Let's go ahead and see Ruby attributes!

Attributes in Ruby Objects

We just talked about Ruby attributes, but we didn't see it yet. Attributes can hold information inside an object and can be used to perform actions, in methods. This attributes is kind of variables but belongs to the Object and you can ask the object about the attributes values.

Again, let's use the MySpecialClass class. Now the message should be seem outside the Ruby object. To make it possible, let's use a special syntax: attr_accessor:

Notice that we used a special method called initialize

This method is being used to give the @message attribute an initial value when the class is created. Again, in another post we will jump into it a little bit more!

if you execute the code, the output will be almost the same:

But now we can access the message attribute outside the object. So, we can call it as the following code:

Great, isn't? We're going to see a lot of examples in another posts!

That's it! In the next post: Part 5 - Ruby Conditionals we're going to see Ruby Conditionals!

I hope that would be useful to you! Thanks!

Follow us to keep up to date!

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Ruby Beginners Tutorial - Ruby Features and Characteristics - Hacking Code

Part 1 - Ruby Language Characteristics

This is the first Post of the Ruby Series! Let’s see the first Ruby Code using IRB!

Ruby Beginners Tutorial - Ruby Methods - Hacking Code

Part 2 - Ruby Methods and Variables

How can we work with Methods and Variables in Ruby? Let’s see!

Ruby Beginners Tutorial - Ruby Strings - Hacking Code

Part 3 - Ruby Strings

It’s really easy to work with Strings in Ruby! Let’s see how!

Ruby Beginners Tutorial - Ruby Objects - Hacking Code

Part 4 - Ruby Classes and Objects

You can’t work without Classes and Objects in Ruby! It’s time to learn how to create them!

Ruby Beginners Tutorial - Ruby Conditionals - Hacking Code

Part 5 - Ruby Conditionals

Conditionals are important in every Programming Language! But how does that work in Ruby?

Ruby Beginners Tutorial - Ruby Arrays - Hacking Code

Part 6 - Ruby Arrays

Arrays in Ruby is really easy and has a lot of great methods! Check it out!

Ruby Beginners Tutorial - Ruby Hashes - Hacking Code

Part 7 - Ruby Hashes

Hashes in Ruby can be used in different scenarios! Let’s see how to use it!

Ruby Beginners Tutorial - Ruby Loops - Hacking Code

Part 8 - Ruby Loops

Ruby Loops to iterate through Arrays and Hashes is really great! Let’s see why!

0 Comments

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: