Ask a Question related to Ruby, Design and Development.
-
Jason Creighton #1
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
-
#39713 [NEW]: Static variables defined in static methods duplicated in derived classes
From: paul at digitalbacon dot us Operating system: Linux, Mac OS X 10.4, Windows XP PHP version: 5.2.0 PHP Bug Type: ... -
#26350 [NEW]: Inherited methods can't access private methods
From: forseti at oak dot rpg dot pl Operating system: Windows 98 SE PHP version: 5.0.0b2 (beta2) PHP Bug Type: Zend Engine 2... -
Aliased setter methods behave differently than other methods?
Here's another question... I am aliasing and redefining certain methods, determined at runtime. Normal methods work fine, but methods ending in '='... -
Help Please: 'Warning 5001 global variable "iF_timer" already defined in global scope
Hi there, I have just started to get this error: 'Warning 5001 global variable "iF_timer" already defined in global scope The variable name... -
How to test a hash of arrays for defined/not defined
"Steve" <ineverlookatthis@yahoo.com> wrote in message news:f0d57f86.0306300420.403e83e7@posting.google.com... I think you want: $chosen =... -
Gavin Sinclair #2
Re: How are global methods defined?
> [Jason:]
Top-level methods become private methods of Object, so I'm told. That>
> 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?
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
-
Jason Creighton #3
Re: How are global methods defined?
On Thu, 11 Sep 2003 15:56:35 +0900
"Gavin Sinclair" <gsinclair@soyabean.com.au> wrote:
But why not add just add private methods to Object if it has the same>> > [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).
effect? What's the point of having Kernel? It seems like going the long
way around.
Jason Creighton
Jason Creighton Guest
-
Gavin Sinclair #4
Re: How are global methods defined?
> On Thu, 11 Sep 2003 15:56:35 +0900
Well, it doesn't cost anything, and it's a good separation of concerns.> "Gavin Sinclair" <gsinclair@soyabean.com.au> wrote:
>>>> top-level like that? Are they just added to Object? And if that's>> > [Jason:]
>> >
>> > But anyway, the question is, what happens to methods defined at the
>> 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
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 Guest
-
Jason Creighton #5
Re: How are global methods defined?
On Fri, 12 Sep 2003 10:54:52 +0900
"Gavin Sinclair" <gsinclair@soyabean.com.au> wrote:
Ah, okay.>> > [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.
Already have it. Allow me to inject a "me too!". 'ri' is so useful it> 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?)
really should be in the distribution.
Jason Creighton
Jason Creighton Guest
-
Michael W Thelen #6
Including 'ri' in Ruby distribution (was: How are global methods defined?)
* Jason Creighton <androflux@softhome.net.remove.to.reply> [2003-09-12 09:49]:
I'll throw my two cents in too... I'm a complete nuby and have been reading the>> > 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.
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>
Michael W Thelen Guest



Reply With Quote

