I ruby 1.8.7, Why I can use require in main, but can’t use self.require?
require('date') # ok
self.require('date')
NoMethodError: private method `require' called for main:Object
from (irb):22
from /usr/lib/ruby/1.8/date.rb:437
Well known that main is Object class:
irb(main):045:0> self
=> main
irb(main):043:0> self.class
=> Object
But I discovered that it have Kernel mixin:
irb(main):042:0> self.class.included_modules
=> [Kernel]
Moreover, I found that require is private method of self:
irb(main):037:0> self.private_methods
=> [... "require", ...]
Same way, I can’t use self.attr_accessor:
irb(main):051:0> class X
irb(main):052:1> self.attr_accessor(:ssss)
irb(main):053:1> end
NoMethodError: private method `attr_accessor' called for X:Class
from (irb):52
from /usr/lib/ruby/1.8/date.rb:437
How does it happend? Can anybody clarify that questions?
Check the following simple example:
In ruby we have implicit and explicit methods receiver, check the next code snippet:
Basically in ruby if a method is
privateit can be called only onimplicitreceiver (withoutselfkeyword). In your examplerequireis aprivatemethod defined theKernelmodule and it can be called only on theimplicitsubject.