Prev: Can't install hpricot gem
Next: fastest readline method
From: Eric Christopherson on 20 Feb 2010 23:11 On Wed, Feb 10, 2010 at 6:02 PM, Eric Christopherson <echristopherson(a)gmail.com> wrote: > Anyway, Charlie, the method is simply 'exit'. It's a class method of > the Kernel module, so it will be invoked if you don't specify a > receiver object. > Sorry to resurrect this, but I've had second thoughts. Are Kernel/Object functions like 'exit' class methods, or instance methods?
From: Robert Klemme on 21 Feb 2010 08:14 On 02/21/2010 05:11 AM, Eric Christopherson wrote: > On Wed, Feb 10, 2010 at 6:02 PM, Eric Christopherson > <echristopherson(a)gmail.com> wrote: >> Anyway, Charlie, the method is simply 'exit'. It's a class method of >> the Kernel module, so it will be invoked if you don't specify a >> receiver object. > > Sorry to resurrect this, but I've had second thoughts. Are > Kernel/Object functions like 'exit' class methods, or instance > methods? This is how a class method looks like: robert(a)fussel:~$ ruby19 -e 'p File.method(:foreach)' #<Method: File(IO).foreach> Now let's check "exit": robert(a)fussel:~$ ruby19 -e 'p method(:exit)' #<Method: Object(Kernel)#exit> Note the "#" instead of the "." before the method name. The first class name is the class of the receiver (the main object in this case) and the second type name is that of the defining type (you can see that in the case of File.foreach above as well). We'll verify that it's an instance method: robert(a)fussel:~$ ruby19 -e 'p Kernel.instance_method(:exit)' #<UnboundMethod: Kernel#exit> If it would not be an instance method, we'd see something similar to this: robert(a)fussel:~$ ruby19 -e 'p File.instance_method(:foreach)' -e:1:in `instance_method': undefined method `foreach' for class `File' (NameError) from -e:1:in `<main>' Now the fun begins: Because of the way Kernel is included in inheritance chains and since everything inherits Object it even appears as a class method: robert(a)fussel:~$ ruby19 -e 'p Kernel.method(:exit)' #<Method: Kernel.exit> Looking at the source code (1.9.1p376) we learn that it is both: In process.c we find these lines rb_define_global_function("exec", rb_f_exec, -1); rb_define_global_function("fork", rb_f_fork, 0); rb_define_global_function("exit!", rb_f_exit_bang, -1); rb_define_global_function("system", rb_f_system, -1); rb_define_global_function("spawn", rb_f_spawn, -1); rb_define_global_function("sleep", rb_f_sleep, -1); rb_define_global_function("exit", rb_f_exit, -1); rb_define_global_function("abort", rb_f_abort, -1); In class.c we see void rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc) { rb_define_module_function(rb_mKernel, name, func, argc); } and then void rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc) { rb_define_private_method(module, name, func, argc); rb_define_singleton_method(module, name, func, argc); } So, finally the answer to your question is "both". :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Brian Candler on 21 Feb 2010 12:24
Robert Klemme wrote: > So, finally the answer to your question is "both". :-) A similar concept you can use is module_function, which can be used as both "class method" of the module itself, and instance method in classes which include the module. module Foo def bar puts "Hello" end module_function :bar end Foo.bar # "Hello" class Baz include Foo def initialize bar end end Baz.new # "Hello" -- Posted via http://www.ruby-forum.com/. |