Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   How are global methods defined? (http://www.velocityreviews.com/forums/t810349-how-are-global-methods-defined.html)

Jason Creighton 09-10-2003 06:20 PM

How are global methods defined?
 
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

Gavin Sinclair 09-11-2003 06:56 AM

Re: How are global methods defined?
 
> [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




Jason Creighton 09-11-2003 08:50 PM

Re: How are global methods defined?
 
On Thu, 11 Sep 2003 15:56:35 +0900
"Gavin Sinclair" <gsinclair@soyabean.com.au> wrote:

> > [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

Gavin Sinclair 09-12-2003 01:54 AM

Re: How are global methods defined?
 
> On Thu, 11 Sep 2003 15:56:35 +0900
> "Gavin Sinclair" <gsinclair@soyabean.com.au> wrote:
>
>> > [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


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




Jason Creighton 09-12-2003 03:42 AM

Re: How are global methods defined?
 
On Fri, 12 Sep 2003 10:54:52 +0900
"Gavin Sinclair" <gsinclair@soyabean.com.au> wrote:

> > [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

Michael W Thelen 09-12-2003 04:48 PM

Including 'ri' in Ruby distribution (was: How are global methods defined?)
 
* Jason Creighton <androflux@softhome.net.remove.to.reply> [2003-09-12 09:49]:
> > 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.


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

--
Michael W. Thelen
It's all magic. :-)
--Larry Wall in <7282@jpl-devvax.JPL.NASA.GOV>



All times are GMT. The time now is 06:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.