Global method usage in standard libs

Discussion in 'Ruby' started by Alexander E. Fischer, Jul 7, 2010.

  1. 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.[](path)
    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 an deprecation
    warning in the old method. Then I would submit this as a patch.
     
    Alexander E. Fischer, Jul 7, 2010
    #1
    1. Advertising

  2. Alexander E. Fischer wrote:
    > Pathname['/home/someone']


    I like this pattern. Note that there is some precedent in the core:
    Array, Hash, and Struct classes all have a [] class method (not
    necessarily equivalent to #new).

    It's also fairly well used in the stdlib and the gems I happen to have
    installed:

    $ ri '[]' | grep -oP '\S+::\[\]\S+'
    WEBrick::HTTPStatus::[],
    Dir::[],
    Array::[],
    Hash::[],
    Set::[],
    Matrix::[],
    Vector::[],
    Generators::AllReferences::[],
    Net::SSH::Version::[],
    YAML::pairs::[],
    YAML::Omap::[],
    Webby::Filters::[],
    Webby::Filters::[],
    CodeRay::FileType::[],
    Pure::[],
    Rake::Task::[],
    Rake::FileList::[],
    JSON::[],
    JSON::[],
    MIME::Types::[],
    OrderedHash::[],
    NArray::[],
    Nokogiri::EncodingHandler::[],
    Nokogiri::HTML::ElementDescription::[],
    Nokogiri::CSS::parser::[]=,
    Nokogiri::CSS::parser::[],
    MultiRBTree::[],


    But note that some of these, like Dir#[], are not the same as #new.

    But I don't know how easy it will be to convince people to live with the
    deprecation warning for Pathname et al. I wouldn't mind it.
     
    Joel VanderWerf, Jul 7, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GlennH
    Replies:
    1
    Views:
    1,136
    Mike Treseler
    Jul 11, 2003
  2. Christoph
    Replies:
    2
    Views:
    542
    Richard Bos
    Sep 17, 2003
  3. =?ISO-8859-1?Q?Une_b=E9vue?=

    [Newbie]where to search for non-standard libs ?

    =?ISO-8859-1?Q?Une_b=E9vue?=, Sep 17, 2006, in forum: C Programming
    Replies:
    5
    Views:
    290
    =?ISO-8859-1?Q?Une_b=E9vue?=
    Sep 17, 2006
  4. Raman
    Replies:
    5
    Views:
    1,072
    Raman
    May 9, 2008
  5. Greg Hauptmann
    Replies:
    4
    Views:
    211
    Stefano Crocco
    Feb 7, 2009
Loading...

Share This Page