Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>At the <a href="http://ruby-doc.org/core-2.0.0/ObjectSpace.html" rel="nofollow"><code>ObjectSpace</code> doc page</a>, note the sentence: "The contents of the returned hash are implementation specific. It may be changed in future." In other words, with <code>ObjectSpace.count_objects</code> you never know, unless you dig deep in the particular Ruby implementation. Let me demonstrate this for you:</p> <pre><code>def sense_changes prev ObjectSpace.count_objects.merge( prev ) { |_, a, b| a - b }.delete_if { |_, v| v == 0 } end prev = ObjectSpace.count_objects # we do absolutely nothing sense_changes( prev ) #=&gt; { :FREE=&gt;-364, :T_OBJECT=&gt;8, :T_STRING=&gt;270, :T_HASH=&gt;11, :T_DATA=&gt;4, :T_MATCH=&gt;11, :T_NODE=&gt;14} </code></pre> <p>And you can keep wondering until the cows come home what the heaven has happened in the <code>ObjectSpace</code> while you have done nothing. As for the <code>:T_CLASS</code> field change by 3, that you observed, Denis's answer applies: 1 is caused by the class itself, 1 by its eigenclass, and 1 by we don't know what (Update: As tlewin has shown, it's the eigenclass's eigenclass). Let me just add that Ruby objects do not have allocated eigenclasses upon creation (Update: As tlewin has shown, classes are an exception to this rule.).</p> <p>Unless you are a core developer, you barely need to wonder about the contents of <code>ObjectSpace.count_objects</code> hash. If you are interested in accessing classes via <code>ObjectSpace</code>, use</p> <pre><code>ObjectSpace.each_object( Class ) </code></pre> <p>Indeed:</p> <pre><code>k = ObjectSpace.each_object( Class ).to_a a = Class.new ObjectSpace.each_object( Class ).to_a.size - k.size #=&gt; 1 ObjectSpace.each_object( Class ).to_a - k == [ a ] #=&gt; true </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload