"Eustaquio Rangel de Oliveira Jr." <eustaquiorangel / yahoo.com> Jan 10, 2005 at 07:00 PM wrote: >I heard that immediate values hold 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 use the same object for all over the program, but Strings, for ex, do not: even if the values are the same, they are distinct objects; right? Moreover, during program execution, each execution of the same line containing a literal String creates a different object: ->for i in (1..2) do a='test'; p a.object_id end 20816438 20816414 ->b=a; b.object_id 20816414 >On the end, are not n1, n2 and n3 references to this [single] object allocated there, shared by all? Are not variables all references, even in the Fixnum case, pointing to an allocated single object there? Fixnum objects are immutable. A variable can refer to different Fixnum's. That's the ideal, and Ruby's behavior. About implementation efficiency and understanding, from _Programming Ruby (1st ed.)_, chapter "Extending Ruby", section "Ruby Objects in C": "[I]mmediate values are not pointers: Fixnum, Symbol, true, false, and nil are stored directly in VALUE. Fixnum values are stored as 31-bit numbers[Or 63-bit on wider CPU architectures.] that are formed by shifting the original number left 1 bit and then setting the least significant bit (bit 0) to ``1.'' When VALUE is used as a pointer to a specific Ruby structure, it is guaranteed always to have an LSB of zero; the other immediate values also have LSBs of zero. Thus, a simple bit test can tell you whether or not you have a Fixnum." ->for i in (1..2) do a=10; p a.object_id end 21 21 ->b=a; p b.object_id; b=nil; b.object_id 21 4 >I think the garbage collector works the same way as other objects on Fixnum, true, false and nil, right? Naturally, it ignores immutable objects. You can create millions of Fixnum objects, yet they take no storage. Not so for String or Bignum objects: ->a=1_000; GC.disable; for i in (1..100_000_000) do b=a*2 end 1..100000000 ->a='z'*1_000; GC.disable; for i in (1..1_000_000) do b=a*2 end (irb):2:in `*': failed to allocate memory (NoMemoryError) ->a=1_000_000_000_000_000_000_000; GC.disable; for i in (1..10_000_000) do b=a*2 end [FATAL] failed to allocate memory ->system 'ruby --version' ruby 1.8.2 (2004-07-29) [i386-mswin32]