Ruby which-like command (rwhich)
Here is a Ruby script to find libraries (like the ones used with require). It is like the Unix which tool.
There is a similar tool here, but this script doesn’t attempt to load the library (which sometimes causes load errors), and this script also locates gem libraries.
#!/usr/bin/env rubyrequire ‘pathname’
EXTENSIONS = %w(.rb .so .o .dll .dylib .class) def which(file) return file if Pathname.new(file).absolute? if EXTENSIONS.any? { |ext| file.end_with?(ext) } search = [ file ] else search = EXTENSIONS.map { |ext| file + ext } end $LOAD_PATH.each do |path| res = search.map { |f| File.join(path, f) }.find { |f| File.exist?(f) } next if res.nil? puts res exit end return nil end
file = ARGV[0] puts “Usage: #{File.filename($0)} [library]” unless file res = which(file)
require ‘rubygems’ if spec = Gem.searcher.find(file) Gem.activate(spec.name, “= #{spec.version}") res = which(file) end
STDERR.puts “#{file}: Library not found.” exit 1