Ruby Programming/Introduction to objects

From Wikibooks, open books for an open world
< Ruby Programming
Jump to navigation Jump to search

← Encoding | Ruby basics →

Like Smalltalk, Ruby is a pure object-oriented language — everything is an object. In contrast, languages such as C++ and Java are hybrid languages that divide the world between objects and primitive types. The hybrid approach results in better performance for some applications, but the pure object-oriented approach is more consistent and simpler to use.

What is an object?[edit | edit source]

Using Smalltalk terminology, an object can do exactly three things.

  1. Hold state, including references to other objects.
  2. Receive a message, from both itself and other objects.
  3. In the course of processing a message, send messages, both to itself and to other objects.

If you don't come from Smalltalk background, it might make more sense to rephrase these rules as follows:

  1. An object can contain data, including references to other objects.
  2. An object can contain methods, which are functions that have special access to the object's data.
  3. An object's methods can call/run other methods/functions.

Variables and objects[edit | edit source]

Let's fire up irb to get a better understanding of objects.

$ irb --simple-prompt
>> comedian = "Stephen Colbert"
=> "Stephen Colbert"
Ruby - Introduction to objects - Variable reference.png

In the first line, we created a String object containing the text "Stephen Colbert". We also told Ruby to use the variable comedian to refer to this object.

Next, we tell Ruby to also use the variable favorite_comedian to refer to the same String object.

>> favorite_comedian = comedian
=> "Stephen Colbert"
Ruby - Introduction to objects - Two variables.png

Now, we have two variables that we can use to refer to the same String object — comedian and favorite_comedian. Since they both refer to the same object, if the object changes (as we'll see below), the change will show up when using either variable.

Methods[edit | edit source]

In Ruby, methods that end with an exclamation mark (also called a "bang") modify the object. For example, the method upcase! changes the letters of a String to uppercase.

>> comedian.upcase!

Since both of the variables comedian and favorite_comedian point to the same String object, we can see the new, uppercase text using either variable.

>> comedian
>> favorite_comedian
Ruby - Introduction to objects - Two variables, modified object.png

Methods that do not end in an exclamation point return data, but do not modify the object. For example, downcase! modifies a String object by making all of the letters lowercase. However, downcase returns a lowercase copy of the String, but the original string remains the same.

>> comedian.downcase
=> "stephen colbert"
>> comedian

Since the original object still contains the text "STEPHEN COLBERT", you might wonder where the new String object, with the lowercase text, went to. Well, after irb printed out its contents, it can no longer be accessed since we did not assign a variable to keep track of it. It's essentially gone, and Ruby will dispose of it.

Reassigning a variable[edit | edit source]

But what if your favorite comedian is not Stephen Colbert? Let's point favorite_comedian to a new object.

>> favorite_comedian = "Jon Stewart"
=> "Jon Stewart"

Now, each variable points to a different object.

Ruby - Introduction to objects - Two variables, two objects.png

Let's say that we change our mind again. Now, our favorite comedian is Ellen DeGeneres.

>> favorite_comedian = "Ellen DeGeneres"
=> "Ellen DeGeneres"

Now, no variable points to the "Jon Stewart" String object any longer. Hence, Ruby will dispose of it.

Ruby - Introduction to objects - Orphaned object.png