Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although I'm not sure I fully understand the question, I'll try to answer:</p> <p>When you're working inside <code>irb</code> you're really working in a context like this:</p> <pre><code>class Object # you're here end </code></pre> <p>So, if you declare a method inside the REPL you're actually declaring it inside <code>main</code>'s class:</p> <pre><code>ruby-1.9.3-rc1 :001 &gt; def foo; end =&gt; nil ruby-1.9.3-rc1 :002 &gt; self.class.public_methods.include?(:foo) =&gt; true </code></pre> <p>I'm not sure where you get the idea that the methods defined there are private to the class (but again, I might be misunderstanding the question).</p> <p>The class you're inside in <code>irb</code> includes the <code>Kernel</code> module so that's why you have access to its functions (which, since you're inside the class, appear as stand-alone functions even though they're regular methods):</p> <pre><code>ruby-1.9.3-rc1 :003 &gt; self.class.included_modules =&gt; [Kernel] </code></pre> <p>This would make it consistent with the typical Ruby behavior and indeed, <code>main</code> is just <code>self</code> inside the Object class.</p> <p>If you call <code>self</code> you'll receive <code>main</code> because that's the way it's instructed to do it through inspect. We can remove the <code>#to_s</code> method from <code>self</code> and see the real value:</p> <pre><code>ruby-1.9.3-p0 :001 &gt; self =&gt; main ruby-1.9.3-p0 :002 &gt; class &lt;&lt; self; remove_method :to_s; end =&gt; #&lt;Class:#&lt;Object:0x007fc08387af00&gt;&gt; ruby-1.9.3-p0 :003 &gt; self =&gt; #&lt;Object:0x007fc08387af00 @prompt={:PROMPT_I=&gt;"ruby-1.9.3-p0 :%03n &gt; ", :PROMPT_S=&gt;"ruby-1.9.3-p0 :%03n%l&gt; ", :PROMPT_C=&gt;"ruby-1.9.3-p0 :%03n &gt; ", :PROMPT_N=&gt;"ruby-1.9.3-p0 :%03n?&gt; ", :RETURN=&gt;" =&gt; %s \n", :AUTO_INDENT=&gt;true}&gt; </code></pre> <p><em>Edit: Before moving forward I think you should clarify which Ruby version are you using, see the sample below for 1.9.3:</em></p> <pre><code>ruby-1.9.3-p0 :001 &gt; def my_method ruby-1.9.3-p0 :002?&gt; puts "method called" ruby-1.9.3-p0 :003?&gt; end =&gt; nil ruby-1.9.3-p0 :004 &gt; ruby-1.9.3-p0 :005 &gt; [].my_method method called =&gt; nil ruby-1.9.3-p0 :006 &gt; Array.my_method method called </code></pre>
    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. 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