Robert Klemme
1/10/2005 2:44:00 PM
"Austin Ziegler" <halostatue@gmail.com> schrieb im Newsbeitrag
news:9e7db91105011004083d19a85f@mail.gmail.com...
> On Mon, 10 Jan 2005 19:00:55 +0900, Eustaquio Rangel de Oliveira Jr.
> <eustaquiorangel@yahoo.com> wrote:
> > I heard that immediate values holds the object, not a reference to
> > it, is that right?
> >
> > I mean:
> >
> > s1 = "test" # a String located on for ex 0xCC53D5DF
> > s2 = s1 # points to the same place as s1
> > s3 = "test" # ANOTHER String, locate on for ex 0xC0DD54D0
> > n1 = 1 # Fixnum here, located on ... ?
> > n2 = n1 # points to the same place as n1
> > n3 = 1 # points to the same place as n1
> >
> > So, Fixnum (as true, false and nil) objects uses the same object
> > for all over the program, but Strings, for ex, does not, even if
> > the value are the same there are distinct objects, right?
> >
> > On the end, n1, n2 and n3 are not reference to this only one
> > object allocated there, shared by all? Variables are not all
> > references, even on the Fixnum case, pointing to an allocated
> > single object there?
>
> Up until this paragraph I was with you and completely agree.
> Variables are all references, even to Fixnum and Symbol objects.
> It's an implementation detail that all Fixnum and Symbol object
> instances s are references to the same object, IMO.
Totally agree! And it doesn't make a difference from the usage point of
view since instances of Symbol, Fixnum, TrueClass, FalseClass, NilClass
are immutable.
Addenum: it's especially noteworthy that strings literals are treated
differently from true, false, Fixnums and Symbols. They create a new
string instance on each evaluation.
>> 5.times { p ["foo", 'foo', :foo, 1, 1.2, 10000000000000, true, false,
nil].map {|o| o.id} }
[134663704, 134663692, 3938574, 3, 134665492, 134665228, 2, 0, 4]
[134661376, 134661316, 3938574, 3, 134665492, 134665228, 2, 0, 4]
[134660428, 134660392, 3938574, 3, 134665492, 134665228, 2, 0, 4]
[134659348, 134659336, 3938574, 3, 134665492, 134665228, 2, 0, 4]
[134656588, 134656372, 3938574, 3, 134665492, 134665228, 2, 0, 4]
=> 5
Kind regards
robert