Aliasing class methods

Hi –

I’d like to override File.open so that it automatically searches for a
file in a list of include paths. I’m trying to use alias, but I can’t
get it to work. How to I do it, please? My code to date is:

class File

alias :old_open open

def File.open(filename, *args, &block)
begin
File.old_open(filename, *args, &block)
rescue Errno::ENOENT
exception = $!
unless $options[:include_path].nil?
$options[:include_path].each do |path|
begin
return File.old_open([path, filename].join(SEPARATOR),
*args, &block)
rescue Errno::ENOENT
end
end
end
raise exception
end
end

end

I get the error:

File.open(‘xxx’, ‘r’) =>

NoMethodError: undefined method old_open' for File:Class from (irb):7:inopen’
from (irb):23
from (irb):3

Although a bit of re-design will avoid me having to override
File.open, I’d like to know how to do it in case it becomes
unavoidable in the future :slight_smile:

Cheers,

Tom

class File

  alias :old_open open

     You have written an alias for an instance method, for a class method
     you need

     class << self
        alias old_open open
     end

  def File.open(filename, *args, &block)

Guy Decoux

Guy has answer, I’ll expand in case you didn’t fully get it, and then
offer an advicelet.

class File
class << File
alias old_open open
end
end

OR

class File
class << self
alias old_open open
end
end

If I were you, I wouldn’t be redefining such an important method, at
least not without damn good reason. I see no reason not to simply add
your own:

class << File
def find_and_open(name, *args, &block) # Want a better name.
path = find_file(name)
File.open(path, *args, &block)
end

def find_file(name)
  # ...
end
private :find_file

end

The resulting code looks more elegant anyway, since there is a
separation of concerns.

Gavin

···

On Wednesday, January 29, 2003, 4:22:14 AM, Tom wrote:

Hi –

I’d like to override File.open so that it automatically searches for a
file in a list of include paths. I’m trying to use alias, but I can’t
get it to work. How to I do it, please? My code to date is:

why does this work with barewords vs. symbols/strings?

-a

···

On Wed, 29 Jan 2003, ts wrote:

 class << self
    alias old_open open
 end

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Gavin Sinclair gsinclair@soyabean.com.au wrote in message news:24926588553.20030129113037@soyabean.com.au

I’d like to override File.open so that it automatically searches for a
file in a list of include paths. I’m trying to use alias, but I can’t
get it to work. How to I do it, please? My code to date is:

If I were you, I wouldn’t be redefining such an important method, at
least not without damn good reason. I see no reason not to simply add
your own:

Guy & Gavin, thanks for the answer.

Gavin, I agree that it’s generally a Bad Idea to override important
methods like File.open. The specific behaviour I wanted in this case
was to cause some third-party code to search for files in a number of
directories, without touching the third party code.

I’m writing a LaTeX build system where input files can reside in any
directories listed in the TEXINPUTS environment variable.

Cheers,

Tom

···

On Wednesday, January 29, 2003, 4:22:14 AM, Tom wrote:

alias is a keyword, #alias_method is a method

Guy Decoux

···

On Wed, 29 Jan 2003, ts wrote:

class << self
alias old_open open
end

why does this work with barewords vs. symbols/strings?

Fair enough. When you perfect it, can you post the code so I can add
it to StandardClassExtensions/IO on the Wiki? (Or you can put it
there yourself, of course.)

Cheers,
Gavin

···

On Friday, January 31, 2003, 5:43:08 AM, Tom wrote:

If I were you, I wouldn’t be redefining such an important method, at
least not without damn good reason. I see no reason not to simply add
your own:

Guy & Gavin, thanks for the answer.

Gavin, I agree that it’s generally a Bad Idea to override important
methods like File.open. The specific behaviour I wanted in this case
was to cause some third-party code to search for files in a number of
directories, without touching the third party code.