From Wikibooks, open books for an open world
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.
Using Smalltalk terminology, an object can do exactly three things.
If you don't come from Smalltalk background, it might make more sense to rephrase these rules as follows:
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 — 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.
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 comedian
and 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.
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3