Project

General

Profile

Actions

Feature #4969

closed

Subtle issue with require

Added by trans (Thomas Sawyer) over 11 years ago. Updated almost 10 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:37757]

Description

If I have a library with same name as Ruby standard library in load path (as an example):

lib/abbrev.rb

There is conflict with loading. Ok, I work around:

require 'rbconfig'

Notice that rubylibdir takes precendence.

LOCATIONS = ::RbConfig::CONFIG.values_at(
'rubylibdir', 'archdir', 'sitelibdir', 'sitearchdir'
)

def require_ruby(file)
LOCATIONS.each do |loadpath|
if path = lib_find(loadpath, file)
return path
end
end

raise LoadError, "no such file to load -- #{fname}"

end

private

SUFFIXES = ['.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar']

# Given a +loadpath+, a file's relative path, +relname+, and 
# options hash, determine a matching file exists. Unless +:load+
# option is +true+, this will check for each viable Ruby suffix.
# If a match is found the full path to the file is returned,
# otherwise +nil+.
def lib_find(loadpath, relname)
  if SUFFIXES.include?(File.extname(relname))
    abspath = File.join(loadpath, relname)
    File.exist?(abspath) ? abspath : nil
  else
    SUFFIXES.each do |ext|
      abspath = File.join(loadpath, relname + ext)
      return abspath if File.exist?(abspath)
    end
  end
  nil
end

Now I can do:

require 'abbrev'
require_ruby 'abbrev'

And it works fine. But, if I do:

require_ruby 'abbrev'
require 'abbrev'

The second is not loaded because somehow it seems to confuse it for the first in $LOADED_FEATURES.

I realize this is a very subtle issue and not likely to effect most people, but it presents a big problem for some of my work (e.g. wedge gem)

How is Ruby confusing the two? Can it be fixed? Or is there at least a work around?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0