Ruby Programming/Introduction to objects
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.
- Hold state, including references to other objects.
- Receive a message, from both itself and other objects.
- 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:
- An object can contain data, including references to other objects.
- An object can contain methods, which are functions that have special access to the object's data.
- 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"
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"
Now, we have two variables that we can use to refer to the same String object —
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! => "STEPHEN COLBERT"
Since both of the variables
favorite_comedian point to the same String object, we can see the new, uppercase text using either variable.
>> comedian => "STEPHEN COLBERT" >> favorite_comedian => "STEPHEN COLBERT"
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 => "STEPHEN COLBERT"
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.
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.