How safe: overridding require

T

Trans

How safe is this?

require 'uri'

module Kernel

alias_method :require_prefacets, :require
def require( *args )
require_esc( *args )
end

def require_esc( fpath )
require_prefacets( File.join( File.dirname( fpath ), URI.escape(
File.basename( fpath ), /\W/ ) ) )
end

end

Better way?

Thanks,
T.
 
N

nobuyoshi nakada

Hi,

At Thu, 4 Aug 2005 21:46:06 +0900,
Trans wrote in [ruby-talk:150707]:
How safe is this?

What do you mean by "safe"?
require 'uri'

module Kernel

alias_method :require_prefacets, :require
def require( *args )
require_esc( *args )
end

def require_esc( fpath )
require_prefacets( File.join( File.dirname( fpath ), URI.escape( File.basename( fpath ), /\W/ ) ) )
end

end

$ irb -ruri
irb(main):001:0> fpath = "foo.rb"
=> "foo.rb"
irb(main):002:0> File.join(File.dirname(fpath), URI.escape(File.basename(fpath), /\W/))
=> "./foo%2Erb"

"foo.rb" and "./foo.rb" are diffrenet for `require', and it
doesn't load files have not suffix.
 
T

Trans

Hello--

nobuyoshi said:
What do you mean by "safe"?

Safe in that it won't cause problems w/ other's work --with Gems, for
instance.
$ irb -ruri
irb(main):001:0> fpath = "foo.rb"
=> "foo.rb"
irb(main):002:0> File.join(File.dirname(fpath), URI.escape(File.basename(fpath), /\W/))
=> "./foo%2Erb"

"foo.rb" and "./foo.rb" are diffrenet for `require', and it
doesn't load files have not suffix.

Thanks. Yes, I discovered soon after posting. Here's my current verson:

require 'uri'

module Kernel

alias_method :require_prefacets, :require
def require( *args )
require_esc( *args )
end

def require_esc( fpath )
fs = fpath.split('/')
fn = fs.pop
dn = fs.join('/')
dn += '/' unless dn.empty?
en = URI.escape( File.basename( fn ), /[^.-_\w]/ )
require_prefacets( "#{dn}#{en}" )
end
alias_method :require_facet, :require_esc

end

Works much better. Should there be any error caching on this sort of
thing?

T.
 
E

evanwebb

The "safety" of this is probably debatable, and I believe so is the use
of alias to solve it. If we have many different librarys changing
require or other core methods, then there is bound to be some problems
eventually. Thats why I wrote maskable.rb, which allows classes to have
there methods masked by other versions of the method. That way, other
implementations of the method can run before the original and can
decide if they are done with the operation, or if the next
implementation should run.

The source is at http://hoshi.fallingsnow.net/svn/maskable/maskable.rb.

Feel free to use it as you wish.
 
T

Trans

The "safety" of this is probably debatable, and I believe so is the use
of alias to solve it. If we have many different librarys changing
require or other core methods, then there is bound to be some problems
eventually. Thats why I wrote maskable.rb, which allows classes to have
there methods masked by other versions of the method. That way, other
implementations of the method can run before the original and can
decide if they are done with the operation, or if the next
implementation should run.

The source is at http://hoshi.fallingsnow.net/svn/maskable/maskable.rb.

Feel free to use it as you wish.

[Sorry I hadn't gone to this sooner].

I must say Evan, I am very impressed by your coding ability. You've
managed to write a very unique, threaded aop-style method wrapping
mechinism. There's quite a bit of expert ruby code techinques in there.
Yet in this instance I think it may be a case of "too smart for one's
own good" ;-) That is too say, I think this is a more than the problem
at hand really requires. I agree with you though, method wrapping would
be the most appropriate way to address augmenting #require, but only if
Ruby supported it natively. I have a number of variant wrapping
mechinisms on file (I've done a lot of AOP for Ruby research), but they
all generally add too much "code-weight" for most uses.

Thanks for the code peek though. I am interesed in AOP wrapping
techinques so having a look at your appraoch was interesting.

T.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top