Thank you. It is a good explanation.
Is there a way to see the object id's of the variable "a" and "string" in
the first statement?
You said it was a good explanation, but your question makes it obvious that
you did not understand it.
Allow me to explain the way I think about these things. This is a
simplification that ignores several key points, but it is a useful mental
model.
We deal with two things in Ruby: names and objects. Objects have IDs and
reference counts but no names, and live in a fuzzy cloud out in memory
somewhere. Names do not have IDs, and live in dictionaries.
A name is bound to exactly one object. An object can be bound to many
names, and it doesn't know which names they are. The object simply exists
without a name.
When you say this in Ruby:
"hello
that creates a nameless string object in the fuzzy cloud. That object has
no references, so it will almost immediately be removed by the garbage
collector.
When you say this:
a = "hello"
that also creates a nameless string object in the fuzzy cloud. That object
is then bound to the name "a". "a" is not an object. It is a name that is
bound to an object. In that example, there is only ONE object. Since the
object itself is nameless, the only way to get to it is through its
bindings. So, to find the object ID of "hello", we have to go through the
name "a".
Now, say I do this:
b = "hello"
This creates a brand new nameless string object in the fuzzy cloud, and
binds that object to the name "b".
Now, say I do this:
c = b
This does NOT create any new objects. Instead, it takes the object bound
to the name "b", and binds it to the name "c". That nameless string object
now has two bindings. It doesn't know the names of its bindings, but it
knows how many there are.
So, when you ask this:
Is there a way to see the object id's of the variable "a" and "string" in
the first statement?
the question doesn't make sense. When you say "a.object_id", you are
asking the name "a" to tell you the object ID of the object to which it is
bound. That IS the object ID of the string. You cannot ask for the object
ID of "string" except to go through one of the names to which it is bound.
When you say
"string".object_id
that creates a brand new object without a name, and tells you its ID. That
object will immediately be deleted, since it has no references.
···
Ralph Shnelvar <ralphs@dos32.com> wrote:
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.