Exit method?

E

Eric Christopherson

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?
 
R

Robert Klemme

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 `<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@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
 
B

Brian Candler

Robert said:
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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top