Florian Frank
11/26/2006 11:05:00 PM
Patrick McNally schrieb:
>> open methed is a method defined in kernel module
>>
>
> Thank you for the quick response.
This answer was not correct. You cannot find the File.open method,
because there is none. It is inherited from the IO class, because File <
IO == true. If you want to find out where a method is located you can
type this into irb:
>> File.method(:open)
# => #<Method: File(IO).open>
This shows the inheritance relation. Ruby's rdoc/ri is a bit stupid, and
can't locate inherited methods so well.
There is also a Kernel#open method, that is mixed into the Object class
as a private method. This makes it possible to open a file by calling
open(filename) do |file|
#...
end
from everywhere. If you call File.open this isn't the method called,
because IO#open is found first:
>> require 'pp'; pp File.ancestors.map { |klass| [ klass,
klass.method(:open) ] }
[[File, #<Method: File(IO).open>],
[IO, #<Method: IO.open>],
[File::Constants, #<Method: Module(Kernel)#open>],
[Enumerable, #<Method: Module(Kernel)#open>],
[Object, #<Method: Class(Kernel)#open>],
[PP::ObjectMixin, #<Method: Module(Kernel)#open>],
[Kernel, #<Method: Kernel.open>]]
You can call Kernel#open only without an explicit receiver, because it
is private. That's why calling, e. g., Array.open would fail with a
NoMethodError.
--
Florian Frank