Calling global method

There really should be a module called global, or
perhaps an object named global, that keeps all the global
variables and methods. That way you could just call
global.method. There should also be syntax sugar to allow you
to say ::method.
“global” methods are private : this is why you can’t call
Object.test

svg% ruby -e ‘def test() end; p Object.test’
-e:1: private method `test’ called for Object:Class (NoMethodError)
svg%
Yes, but that’s not how globals usually behave. Globals are
usually always globally available unless overridden or removed.

Does Ruby need globals, especially given that Kernel already
exists?

-austin

···


austin ziegler * austin.ziegler@evault.com

Kernel is just another way of saying global. How you implement globals
doesn’t change the concept of globals itself. Whether they’re held in a
nameless or named space, they’re still globals.

But it seems to me that Kernel ended up being the global namespace after a
couple paradigm changes, and there isn’t much consistency about them. Some
globals are private members of Object and some are in Kernel. You can access
global variables anywhere using $, but you can’t access all global methods
from anywhere. You can access Kernel::method globals, if they’re in Kernel,
but if they’re a private member of Object, you have to perform some strange
tricks.

It would be nice of all globals were wrapped up in a Global module, so you
could access them directly from anywhere. Variables could still be sugared
with $, and methods could be sugared with ::.

Sean O'Dell
···

On Friday 28 May 2004 11:56, Austin Ziegler wrote:

There really should be a module called global, or
perhaps an object named global, that keeps all the global
variables and methods. That way you could just call
global.method. There should also be syntax sugar to allow you
to say ::method.
“global” methods are private : this is why you can’t call
Object.test

svg% ruby -e ‘def test() end; p Object.test’
-e:1: private method `test’ called for Object:Class (NoMethodError)
svg%

Yes, but that’s not how globals usually behave. Globals are
usually always globally available unless overridden or removed.

Does Ruby need globals, especially given that Kernel already
exists?

Hi –

There really should be a module called global, or
perhaps an object named global, that keeps all the global
variables and methods. That way you could just call
global.method. There should also be syntax sugar to allow you
to say ::method.
“global” methods are private : this is why you can’t call
Object.test

svg% ruby -e ‘def test() end; p Object.test’
-e:1: private method `test’ called for Object:Class (NoMethodError)
svg%

Yes, but that’s not how globals usually behave. Globals are
usually always globally available unless overridden or removed.

Does Ruby need globals, especially given that Kernel already
exists?

Kernel is just another way of saying global. How you implement globals
doesn’t change the concept of globals itself. Whether they’re held in a
nameless or named space, they’re still globals.

Yes, but whether you implement globals does change the question of
whether you should be judged, as a language, on the cleanness of your
implementation of globals :slight_smile: You’re assuming that Ruby is somehow
accountable for implementing globals, and then (not surprisingly)
finding that it does so inadequately. But there’s no reason to hold
Ruby to that contract in the first place.

But it seems to me that Kernel ended up being the global namespace after a
couple paradigm changes, and there isn’t much consistency about them. Some
globals are private members of Object and some are in Kernel. You can access
global variables anywhere using $, but you can’t access all global methods
from anywhere. You can access Kernel::method globals, if they’re in Kernel,
but if they’re a private member of Object, you have to perform some strange
tricks.

No stranger than with other private methods. The only singularity, I
think, is the fact that methods written at the top-level are given
this status in the first place (and they had to go somewhere :slight_smile:
Beyond that, everything grows very organically out of the basic
premises of Ruby. For example, this:

class Object
private
def x; end
end

(or just “def x; end” at the top level, with the wrapper implicit) is
very similar to this:

class C
private
def y; end
end

with respect to what you have to do later to get at the method.

Similarly, Kernel – though obviously a very important module – is
just part of the module and mixin mechanism of Ruby, and behaves
accordingly.

What results from all of this may look somewhat like global-ness, but
it isn’t, nor does it claim to be. It’s just Ruby being Ruby.

David

···

On Sat, 29 May 2004, Sean O’Dell wrote:

On Friday 28 May 2004 11:56, Austin Ziegler wrote:


David A. Black
dblack@wobblini.net

[I meant to post this to clr in the first place… it may show up twice on ruby-talk]

Hi –

There really should be a module called global, or
perhaps an object named global, that keeps all the global
variables and methods. That way you could just call
global.method. There should also be syntax sugar to allow you
to say ::method.
“global” methods are private : this is why you can’t call
Object.test

svg% ruby -e ‘def test() end; p Object.test’
-e:1: private method `test’ called for Object:Class (NoMethodError)
svg%

Yes, but that’s not how globals usually behave. Globals are
usually always globally available unless overridden or removed.

Does Ruby need globals, especially given that Kernel already
exists?

Kernel is just another way of saying global. How you implement globals
doesn’t change the concept of globals itself. Whether they’re held in a
nameless or named space, they’re still globals.

Yes, but whether you implement globals does change the question of
whether you should be judged, as a language, on the cleanness of your
implementation of globals :slight_smile: You’re assuming that Ruby is somehow
accountable for implementing globals, and then (not surprisingly)
finding that it does so inadequately. But there’s no reason to hold
Ruby to that contract in the first place.

But it seems to me that Kernel ended up being the global namespace
after a couple paradigm changes, and there isn’t much consistency
about them. Some globals are private members of Object and some are
in Kernel. You can access global variables anywhere using $, but
you can’t access all global methods from anywhere. You can access
Kernel::method globals, if they’re in Kernel, but if they’re a
private member of Object, you have to perform some strange tricks.

No stranger than with other private methods. The only singularity, I
think, is the fact that methods written at the top-level are given
this status in the first place (and they had to go somewhere :slight_smile:
Beyond that, everything grows very organically out of the basic
premises of Ruby. For example, this:

class Object
private
def x; end
end

(or just “def x; end” at the top level, with the wrapper implicit) is
very similar to this:

class C
private
def y; end
end

with respect to what you have to do later to get at the method.

Similarly, Kernel – though obviously a very important module – is
just part of the module and mixin mechanism of Ruby, and behaves
accordingly.

What results from all of this may look somewhat like global-ness, but
it isn’t, nor does it claim to be. It’s just Ruby being Ruby.

David

···

On Sat, 29 May 2004, Sean O’Dell wrote:

On Friday 28 May 2004 11:56, Austin Ziegler wrote:


David A. Black
dblack@wobblini.net

I went with what was obvious to me, and what I found “the least surprising.”
When I look at a blank text editor, and type:

def method
end

…I expect that to be a global. It looks global.

It would be better if either you simply weren’t allowed to do that, and you
were FORCED to specify a module or class before definining any methods, such
as Java does, or the methods are simply placed into Kernel by default, making
them global. To me, it’s completely unexpected that such a small bit of code
such as the above would be so hard to call from within other module methods.

Sean O'Dell
···

On Friday 28 May 2004 15:31, David A. Black wrote:

Yes, but whether you implement globals does change the question of
whether you should be judged, as a language, on the cleanness of your
implementation of globals :slight_smile: You’re assuming that Ruby is somehow
accountable for implementing globals, and then (not surprisingly)
finding that it does so inadequately. But there’s no reason to hold
Ruby to that contract in the first place.

Without getting into whether this is good, bad, indifferent, or Just
Ruby, I must say that I found it quite strange.

Most would naturally expect the top level to behave as if you were in
some top-level module. But it’s a completely different beast. The
fact that you are in an instance of Object is the strange part.

Could someone give a good reason why the top level is an instance of
Object rather than a module with “extend self”?

···

— Sean O’Dell sean@celsoft.com wrote:

def method
end

…I expect that to be a global. It looks global.

It would be better if either you simply weren’t allowed to do that, and you
were FORCED to specify a module or class before definining any methods, such
as Java does, or the methods are simply placed into Kernel by default, making
them global. To me, it’s completely unexpected that such a small bit of code
such as the above would be so hard to call from within other module methods.


Do you Yahoo!?
Friends. Fun. Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/

def method
end

...I expect that to be a global. It looks global.

Use 1.4.x

svg% ruby -ve 'def test() puts "test"; end; class A; Object.test; end'
ruby 1.4.2 (1999-09-18) [i686-linux]
test
svg%

Guy Decoux

Can I return objects from blocks using next in 1.4? Or a myriad of other
additions to the language since 1.4? If so, then I can use 1.4. Otherwise,
it’s not a useful suggestion.

Sean O'Dell
···

On Saturday 29 May 2004 04:04, ts wrote:

def method
end

…I expect that to be a global. It looks global.

Use 1.4.x