Exit method?

On Wed, Feb 10, 2010 at 6:02 PM, Eric C.
[email protected] 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?

On 02/21/2010 05:11 AM, Eric C. wrote:

On Wed, Feb 10, 2010 at 6:02 PM, Eric C.
[email protected] 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@fussel:~$ ruby19 -e ‘p File.method(:foreach)’
#<Method: File(IO).foreach>

Now let’s check “exit”:

robert@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@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@fussel:~$ ruby19 -e ‘p File.instance_method(:foreach)’
-e:1:in instance_method': undefined method foreach’ for class File' (NameError) from -e:1:in

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@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”. :slight_smile:

Kind regards

robert

Robert K. wrote:

So, finally the answer to your question is “both”. :slight_smile:

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”