How are global methods defined?

J

Jason Creighton

Hi,

How are global methods defined in Ruby? I know you can just

def just_a_method(arg)
puts "do stuff"
end

....and you can call it anywhere. I had thought that those methods are
automagically added to Kernel, but appearently that's only methods
defined from C with rb_define_global_function (which just adds the
method to Kernel), or those methods define in Kernel like so

class << Kernel
def just_a_method(arg)
puts "do stuff"
end
end

or:

module Kernel
def Kernel.just_a_method(arg)
puts "do stuff"
end
end

But anyway, the question is, what happens to methods defined at the
top-level like that? Are they just added to Object? And if that's the
case, why do we even have Kernel?

Jason Creighton
 
G

Gavin Sinclair

[Jason:]
But anyway, the question is, what happens to methods defined at the
top-level like that? Are they just added to Object? And if that's the
case, why do we even have Kernel?

Top-level methods become private methods of Object, so I'm told. That
should make them invisible to subclasses. Ho hum.

Anyway, we have Kernel so that lots of methods can be gathered together
and made available (via inclusion in Object).

It is my opinion that Kernel should only contain genuine kernel-type
methods (i.e. those that only the Ruby interpreter can provide, like
at_exit, autoload, caller, local_variables, ...). About half of Kernel is
simply convenience methods (sprintf, Array, loop, ...) which could easily
be implemented in Ruby. These should be in a "Convenience" module, which
is also included in Object. Not that it's a big deal, of course.

You should never feel the need to add anything to Kernel.

Gavin
 
J

Jason Creighton

[Jason:]

But anyway, the question is, what happens to methods defined at the
top-level like that? Are they just added to Object? And if that's the
case, why do we even have Kernel?

Top-level methods become private methods of Object, so I'm told. That
should make them invisible to subclasses. Ho hum.

Anyway, we have Kernel so that lots of methods can be gathered together
and made available (via inclusion in Object).

But why not add just add private methods to Object if it has the same
effect? What's the point of having Kernel? It seems like going the long
way around.

Jason Creighton
 
G

Gavin Sinclair

[Jason:]

But anyway, the question is, what happens to methods defined at the
top-level like that? Are they just added to Object? And if that's
the case, why do we even have Kernel?

Top-level methods become private methods of Object, so I'm told. That
should make them invisible to subclasses. Ho hum.

Anyway, we have Kernel so that lots of methods can be gathered
together and made available (via inclusion in Object).

But why not add just add private methods to Object if it has the same
effect? What's the point of having Kernel? It seems like going the long
way around.

Jason Creighton[/QUOTE]

Well, it doesn't cost anything, and it's a good separation of concerns.
The stuff in Kernel doesn't conceptually belong in Object. Yes, it causes
some pain and suffering when you can't find the documentation :) But
every Rubyist learns about Kernel pretty soon. And it's worth it in the
long run to avoid bloat and cognitive dissonance.

If I could hand down only one piece of advice to a new Rubyist, it's this:
get 'ri' (why oh why is this not in the distribution?) and read most of
the things you can find in there. Then you know what is built in to Ruby
and what it not, and you know where to find the most important things.

Here is the end of the output from running 'ri':

'ri' has documentation for the classes and modules:

Array, Bignum, Binding, Class, Comparable, Continuation, Dir,
Enumerable, Errno, Exception, FalseClass, File, File::Stat,
FileTest, Fixnum, Float, GC, Hash, IO, Integer, Kernel, Marshal,
MatchData, Math, Method, Module, NilClass, Numeric, Object,
ObjectSpace, Proc, Process, Process::Status, Range, Regexp, Signal,
String, Struct, Struct::Tms, Symbol, Thread, ThreadGroup, Time,
TrueClass, UnboundMethod

All of these are worth knowing about in some detail.

Cheers,
Gavin
 
J

Jason Creighton

[Jason Creighton]:
But why not add just add private methods to Object if it has the same
effect? What's the point of having Kernel? It seems like going the long
way around.

Well, it doesn't cost anything, and it's a good separation of concerns.
The stuff in Kernel doesn't conceptually belong in Object. Yes, it causes
some pain and suffering when you can't find the documentation :) But
every Rubyist learns about Kernel pretty soon. And it's worth it in the
long run to avoid bloat and cognitive dissonance.

Ah, okay.
If I could hand down only one piece of advice to a new Rubyist, it's this:
get 'ri' (why oh why is this not in the distribution?)

Already have it. Allow me to inject a "me too!". 'ri' is so useful it
really should be in the distribution.

Jason Creighton
 
M

Michael W Thelen

* Jason Creighton said:
Already have it. Allow me to inject a "me too!". 'ri' is so useful it
really should be in the distribution.

I'll throw my two cents in too... I'm a complete nuby and have been reading the
Pickaxe book and subscribing to ruby-talk for a while. But it wasn't until I
got 'ri' and started using it that I've started feeling at all confident in my
understanding of Ruby. It was a little confusing when one of the tutorials (I
don't remember which one) suggested using 'ri', but but it took a while just to
figure out where to get it. I think including it in the distribution would be
a great benefit.

-- Mike
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top