January 2024
Intermediate to advanced
718 pages
20h 15m
English
Now that we’ve gone through the trouble of creating all these objects, let’s make sure we don’t lose them. Variables are used to keep track of objects; each variable holds a reference to an object. Let’s confirm this with some code:
| | person = "Tim" |
| | puts "The object in 'person' is a #{person.class}" |
| | puts "The object has an id of #{person.object_id}" |
| | puts "and a value of '#{person}'" |
Produces:
| | The object in 'person' is a String |
| | The object has an id of 60 |
| | and a value of 'Tim' |
On the first line, Ruby creates a new string object with the value Tim. A reference to this object is placed in the local variable person. A quick check shows that the variable has indeed taken on the personality of a string, with ...
Read now
Unlock full access