Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following solution refers to the superclass of the eigenclass. As a consequence, it has the <strong>side effect</strong> of allocating the eigenclass (detectable by <code>ObjectSpace.count_objects[:T_CLASS]</code> in MRI). But since <code>BasicObject#class</code> is only invoked on blank slate objects (i.e. objects that are not kind-of <code>Object</code>, i.e. that are not <code>Object</code><i>s</i>) the side effect also applies just for blank slate objects. For <code>Object</code><i>s</i>, the standard <code>Kernel#class</code> is invoked.</p> <pre><code>class BasicObject def class (class &lt;&lt; self; self end).superclass end end # tests: puts RUBY_VERSION # 1.9.2 class B &lt; BasicObject; end class X; end p BasicObject.new.class # BasicObject p B .new.class # B p X .new.class # X p 6.class # Fixnum p B.instance_method(:class).owner # BasicObject p X.instance_method(:class).owner # Kernel p 6.method(:class).owner # Kernel </code></pre> <p>Edit - Note: Indeed, there is an issue with <code>ActiveSupport::Duration</code>. This class uses interception (<code>method_missing</code>) for redirecting messages to the <code>:value</code> attribute. As a consequence, it provides false introspection for its instances. To preserve this falsity, it is necessary to use another name for the class map, e.g. the proposed <code>__realclass__</code>. Thus, the modified solution might look like this:</p> <pre><code>class BasicObject def __realclass__; (class &lt;&lt; self; self end).superclass end end class Object; alias __realclass__ class end </code></pre> <p>Another way of not invoking <code>class &lt;&lt; self</code> on <code>Object</code>s is via <code>Module#===</code>, as suggested by Kelvin on this page.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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