Dear RubyGems: Perhaps a better way to override require...

T

Trans

I may have found a much better way to override Kernel#require. Here's
my current code:

require 'uri'

module Kernel::NanoKernel

FILE_ESC_RE = %r{[^a-zA-Z_0-9.\-]}

def require( fpath )
fs = fpath.split('/')
fn = fs.pop
dn = fs.join('/')
dn += '/' unless dn.empty?
en = URI.escape( File.basename( fn ), FILE_ESC_RE )
super( "#{dn}#{en}" )
end

end

class Object
include NanoKernel
end

I appears to work flawlessly. Agree? Or do you see a bug waiting to
bite?

Thanks,
T.
 
C

Chad Fowler

I may have found a much better way to override Kernel#require. Here's
my current code:
=20
require 'uri'
=20
module Kernel::NanoKernel
=20
FILE_ESC_RE =3D %r{[^a-zA-Z_0-9.\-]}
=20
def require( fpath )
fs =3D fpath.split('/')
fn =3D fs.pop
dn =3D fs.join('/')
dn +=3D '/' unless dn.empty?
en =3D URI.escape( File.basename( fn ), FILE_ESC_RE )
super( "#{dn}#{en}" )
end
=20
end
=20
class Object
include NanoKernel
end
=20
I appears to work flawlessly. Agree? Or do you see a bug waiting to
bite?

Tom, what problem are you trying to solve? I mean, I see that you're
overriding require, but I'm not sure how it's supposed to be better.=20
I'm not implying that it _isn't_. Just trying to understand the
intent.
--=20
Chad Fowler
http://chadfowler.com
http://rubycentral.org=20
http://rubygarden.org=20
http://rubygems.rubyforge.org (over 700,000 gems served!)
 
T

Trans

Hi Chad,

I have a library that allows one to require individual methods by name.
Unfortuantely some file systems have punctuation limitations on file
names, so I need to "escape" the names and then override Kernel#require
to take that into account. I did it the tradiional way using alias, but
for that caused conflicts with RubyGems, which does the same thing. In
figuring out what to do about it I hit on the above solution.

I simply thought you might like to know about this, since it seems to
be a more robust solution. Also, I just wanted to be sure I didn't
overlook something, i.e. if anyone else sees a problem this way of
doing it.

Thanks,
T.
 
C

Chad Fowler

Hi Chad,
=20
I have a library that allows one to require individual methods by name.
Unfortuantely some file systems have punctuation limitations on file
names, so I need to "escape" the names and then override Kernel#require
to take that into account. I did it the tradiional way using alias, but
for that caused conflicts with RubyGems, which does the same thing. In
figuring out what to do about it I hit on the above solution.
=20
I simply thought you might like to know about this, since it seems to
be a more robust solution. Also, I just wanted to be sure I didn't
overlook something, i.e. if anyone else sees a problem this way of
doing it.
=20

Ahh, I get it. The point (that I missed) wasn't the actual
implementation of require
but the fact that you were overriding it using a mixin instead of an
alias. Seems reasonable. I don't see any obvious problems either,
but I'd let the question linger here....anyone else?

--=20
Chad Fowler
http://chadfowler.com
http://rubycentral.org=20
http://rubygarden.org=20
http://rubygems.rubyforge.org (over 700,000 gems served!)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top