Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This <strong>is</strong> possible.</p> <pre><code>class Object class &lt;&lt; self Kernel.send :define_method, :foo do #Object.send to be more restrictive @foo = 'bar' end end end </code></pre> <p>.</p> <pre><code>irb(main):023:0&gt; class Object irb(main):024:1&gt; class &lt;&lt; self irb(main):025:2&gt; Kernel.send :define_method, :foo do irb(main):026:3* @foo = 'bar' irb(main):027:3&gt; end irb(main):028:2&gt; end irb(main):029:1&gt; end =&gt; #&lt;Proc:0x00007f5ac89db168@(irb):25&gt; irb(main):030:0&gt; x = Object.new =&gt; #&lt;Object:0x7f5ac89d6348&gt; irb(main):031:0&gt; x.foo =&gt; "bar" irb(main):032:0&gt; [].foo =&gt; "bar" irb(main):033:0&gt; //.foo =&gt; "bar" </code></pre> <p>It's important to understand eigenclasses. Every class is implemented mysteriously by the Ruby interpreter as a hidden class type:</p> <pre><code>irb(main):001:0&gt; class Object irb(main):002:1&gt; def eigenclass irb(main):003:2&gt; class &lt;&lt; self; self; end irb(main):004:2&gt; end irb(main):005:1&gt; end =&gt; nil irb(main):006:0&gt; x = Object.new =&gt; #&lt;Object:0x7fb6f285cd10&gt; irb(main):007:0&gt; x.eigenclass =&gt; #&lt;Class:#&lt;Object:0x7fb6f285cd10&gt;&gt; irb(main):008:0&gt; Object.eigenclass =&gt; #&lt;Class:Object&gt; irb(main):009:0&gt; Object.class =&gt; Class </code></pre> <p>So, in your example, when you're trying to call foo on your objects, it's operating on the class <code>#&lt;Object:0x7fb6f285cd10&gt;</code>. However, what you want it to operate on is the secret class <code>#&lt;Class:#&lt;Object:0x7fb6f285cd10&gt;&gt;</code></p> <p>I hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    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.
    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