Although I'm not sure I fully understand the question, I'll try to answer:
When you're working inside irb
you're really working in a context like this:
class Object
# you're here
end
So, if you declare a method inside the REPL you're actually declaring it inside main
's class:
ruby-1.9.3-rc1 :001 > def foo; end
=> nil
ruby-1.9.3-rc1 :002 > self.class.public_methods.include?(:foo)
=> true
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).
The class you're inside in irb
includes the Kernel
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):
ruby-1.9.3-rc1 :003 > self.class.included_modules
=> [Kernel]
This would make it consistent with the typical Ruby behavior and indeed, main
is just self
inside the Object class.
If you call self
you'll receive main
because that's the way it's instructed to do it through inspect. We can remove the #to_s
method from self
and see the real value:
ruby-1.9.3-p0 :001 > self
=> main
ruby-1.9.3-p0 :002 > class << self; remove_method :to_s; end
=> #<Class:#<Object:0x007fc08387af00>>
ruby-1.9.3-p0 :003 > self
=> #<Object:0x007fc08387af00 @prompt={:PROMPT_I=>"ruby-1.9.3-p0 :%03n > ", :PROMPT_S=>"ruby-1.9.3-p0 :%03n%l> ", :PROMPT_C=>"ruby-1.9.3-p0 :%03n > ", :PROMPT_N=>"ruby-1.9.3-p0 :%03n?> ", :RETURN=>" => %s \n", :AUTO_INDENT=>true}>
Edit: Before moving forward I think you should clarify which Ruby version are you using, see the sample below for 1.9.3:
ruby-1.9.3-p0 :001 > def my_method
ruby-1.9.3-p0 :002?> puts "method called"
ruby-1.9.3-p0 :003?> end
=> nil
ruby-1.9.3-p0 :004 >
ruby-1.9.3-p0 :005 > [].my_method
method called
=> nil
ruby-1.9.3-p0 :006 > Array.my_method
method called