Calling global method

That is, prefix the method name by a double colon as you would a
constant defined outside of any module [1]. It doesn’t work, though.
Some mechanism like that would be awfully useful in certain cases
(I’ve
personally come across such a case in a real application), and could
be
implemented much less hackishly than Ruby currently allows.
Specifically, what’s needed is a way to access the top-level “module”
within which methods are defined (or are they defined in Object by
default?).

IIUC, there is no “top-level module”. Ruby emulates it for your
convenience if you program a perl-like script be defining all your
methods as private in Object. Thus you can always call them (as you are
always in the same scope).

If you start using Modules (and Classes) than you’re probably after an
OO design. Why would you then put things as “global” and not in a
dedicated namespace/class? The only time you may want to do this is when
you need it from within each object (or for convenience, like Kernel).
If you want to break this paradigm, Ruby lets you do this by calling the
TOPLEVEL_BINDING.

This demostrates two of Ruby’s principles:

  • You can do what you want.
  • The language has a hidden message about a certain (better) way.

I didn’t put this particular method in the global space. It’s in one of the
built-in libraries.

Besides, you should be able to have globals, even if you partition your code
into namespaces.

Sean O'Dell
···

On Thursday 27 May 2004 22:34, Mehr, Assaph (Assaph) wrote:

If you start using Modules (and Classes) than you’re probably after an
OO design. Why would you then put things as “global” and not in a
dedicated namespace/class? The only time you may want to do this is when
you need it from within each object (or for convenience, like Kernel).
If you want to break this paradigm, Ruby lets you do this by calling the
TOPLEVEL_BINDING.