Ruby Programming/Introduction to objects

Like Smalltalk, Ruby is a pure object-oriented language &mdash; 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?
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
Let's fire up  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 " ". We also told Ruby to use the variable  to refer to this object.

Next, we tell Ruby to also use the variable  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 &mdash;  and. 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
In Ruby, methods that end with an exclamation mark (also called a "bang") modify the object. For example, the method  changes the letters of a String to uppercase.

>> comedian.upcase! => "STEPHEN COLBERT"

Since both of the variables  and   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,  modifies a String object by making all of the letters lowercase. However,  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 " ", you might wonder where the new String object, with the lowercase text, went to. Well, after  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
But what if your favorite comedian is not Stephen Colbert? Let's point  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 " " String object any longer. Hence, Ruby will dispose of it.