How are global methods defined?

Discussion in 'Ruby' started by Jason Creighton, Sep 10, 2003.

  1. 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
     
    Jason Creighton, Sep 10, 2003
    #1
    1. Advertising

  2. > [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
     
    Gavin Sinclair, Sep 11, 2003
    #2
    1. Advertising

  3. On Thu, 11 Sep 2003 15:56:35 +0900
    "Gavin Sinclair" <> 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
     
    Jason Creighton, Sep 11, 2003
    #3
  4. > On Thu, 11 Sep 2003 15:56:35 +0900
    > "Gavin Sinclair" <> 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
     
    Gavin Sinclair, Sep 12, 2003
    #4
  5. On Fri, 12 Sep 2003 10:54:52 +0900
    "Gavin Sinclair" <> 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
     
    Jason Creighton, Sep 12, 2003
    #5
  6. Including 'ri' in Ruby distribution (was: How are global methods defined?)

    * Jason Creighton <> [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 <>
     
    Michael W Thelen, Sep 12, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  2. Replies:
    1
    Views:
    506
    Sion Arrowsmith
    Jul 10, 2008
  3. M. Ayhan
    Replies:
    1
    Views:
    118
    Trans
    Mar 8, 2007
  4. Kenneth McDonald
    Replies:
    5
    Views:
    326
    Kenneth McDonald
    Sep 26, 2008
  5. User1014
    Replies:
    3
    Views:
    192
    Richard Cornford
    Dec 1, 2006
Loading...

Share This Page