# Global methods in Ruby standard lib

**URL:** <https://www.ruby-forum.com/t/global-methods-in-ruby-standard-lib/191203>\
**Category:** Ruby\
**Created:** [July 7, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/global-methods-in-ruby-standard-lib/191203 "2010-07-07T14:55:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_ESFische](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9bd4f/32.png) [@Alexander\_ESFische](https://www.ruby-forum.com/u/Alexander_ESFische)\
**Post date:** [July 7, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/global-methods-in-ruby-standard-lib/191203/1 "2010-07-07T14:55:45Z")

</div>

I noticed that at least some of the standard libs use global methods to  
provide some kind of shortcut to the actual new method. For example the  
excellent Pathname class’ objects can be generated either by

Pathname.new(’/home/someone’)

or simply through

Pathname(’/home/someone’)

The latter example is possible through a method on the Kernel module,  
which is named capitalized.

module Kernel  
def Pathname(path)  
Pathname.new(path)  
end  
end

I think this shortcut is really useful, but also very ugly in its  
implementation. If more classes do this, especially classes outside of  
the class library, the global method namespace, which is already filled  
with a lot of things, will become overcrowded. In the end there will be  
naming collisions because of a lack of sub-namespacing. To discourage  
this behaviour I would recommend to remove those global methods and  
implement the whole thing in a slightly different fashion:

class Pathname  
def self.  
new(path)  
end  
end

This implementation would allow this:

Pathname[’/home/someone’]

which is still very near to the current variant but more eco-friendly in  
my opinion.

If there is no big problem about this (tell me, if you see one!) I’m  
willing to add the new methods in affected classes (at least the  
standard  
lib classes Complex and Rational use this, too) and add a deprecation  
warning in the old methods. Then I would submit this as a patch.

---

<div class="post-metadata">

**Author:** ![Alexander\_ESFische](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9bd4f/32.png) [@Alexander\_ESFische](https://www.ruby-forum.com/u/Alexander_ESFische)\
**Post date:** [July 7, 2010, 3:06pm UTC](https://www.ruby-forum.com/t/global-methods-in-ruby-standard-lib/191203/2 "2010-07-07T15:06:18Z")

</div>

On Wed, 07 Jul 2010 12:52:25 +0000, Alexander E. Fischer wrote:

> The latter example is possible through a method on the Kernel module,  
> the class library, the global method namespace, which is already filled
> 
> deprecation warning in the old methods. Then I would submit this as a  
> patch.

Sorry for the repost. Do NOT use this thread to discuss. Use the other  
thread called “Global method usage in standard libs” for your answers.

Thank you.
