Professional Web Applications Themes

How are global methods defined? - Ruby

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

  1. #1

    Default 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
    Jason Creighton Guest

  2. #2

    Default 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



    Gavin Sinclair Guest

  3. #3

    Default Re: How are global methods defined?

    On Thu, 11 Sep 2003 15:56:35 +0900
    "Gavin Sinclair" <gsinclairsoyabean.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
    Jason Creighton Guest

  4. #4

    Default Re: How are global methods defined?

    > On Thu, 11 Sep 2003 15:56:35 +0900
    > "Gavin Sinclair" <gsinclairsoyabean.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 doentation :) 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 doentation 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 Guest

  5. #5

    Default Re: How are global methods defined?

    On Fri, 12 Sep 2003 10:54:52 +0900
    "Gavin Sinclair" <gsinclairsoyabean.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 doentation :) 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 Guest

  6. #6

    Default Including 'ri' in Ruby distribution (was: How are global methods defined?)

    * Jason Creighton <androfluxsofthome.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 <7282jpl-devvax.JPL.NASA.GOV>

    Michael W Thelen Guest

Similar Threads

  1. Replies: 2
    Last Post: December 3rd, 12:47 AM
  2. #26350 [NEW]: Inherited methods can't access private methods
    By forseti at oak dot rpg dot pl in forum PHP Development
    Replies: 0
    Last Post: November 21st, 02:31 PM
  3. Replies: 8
    Last Post: July 18th, 04:34 PM
  4. Replies: 0
    Last Post: July 17th, 09:23 AM
  5. How to test a hash of arrays for defined/not defined
    By Sisyphus in forum PERL Miscellaneous
    Replies: 1
    Last Post: June 30th, 01:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139