From: Todd Benson Date: 2007-10-01T06:52:21+09:00 Subject: Re: ObjectSpace reveals order? On 9/29/07, Eric Hodel wrote: > On Sep 28, 2007, at 18:18 , Todd Benson wrote: > > > If I do... > > > > ObjectSpace.each_object { |o| puts o; gets } > > > > will it give me the consecutive order of the loading of the objects > > (i.e. the time order that memory is allocated on the heap)? > > Well, its kinda backwards*: > > $ cat test.rb > items = ['a', 'b'] > > ObjectSpace.each_object do |o| > p o.object_id => o > end > > $ ruby test.rb | head -3 > {1001920=>"b"} > {1001930=>"a"} > {1001940=>["a", "b"]} > > [] was allocated first, then 'a', then 'b', not the other way around. Ok, logistically this makes sense. I see. > > * This example was too small to invoke the garbage collector, so it > will be in reverse order. With garbage collection you aren't > guaranteed any ordering. > > > Or is it a tree structure underneath or something strange like that? > > You can infer the structure of ruby's heap if you make enough > objects. Replace items = ['a', 'b'] with ('a'..'aaa').to_a and add > GC.disable OK. Got ya. > Ruby's heaps look something like: > > heaps = [ > [ ... ], # objects live in here > ... # more arrays of objects, as necessary > ] > > This was on OS X. > > > Why do I get String objects right off the bat like... > > > > ' does not evaluate to a gem specification > > You have RUBYOPT=-rubygems Thanks! Todd