Open the ruby ​​require file to be downloaded?

The requireruby method will look for lib_path and load the first matching files, if necessary. Is there a way to print the path to the file to be downloaded. I am looking for perfectly built-in functionality similar to a command whichin bash, and I hope that this can also be that simple. Thank.

+3
source share
2 answers

I do not know the built-in functionality, but defining your own is not difficult. Here's a solution adapted from this question :

def which(string)
  $:.each do |p|
    if File.exist? File.join(p, string)
      puts File.join(p, string)
      break
    end
  end
end

which 'nokogiri'
#=> /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri

: $: - . , load require. which , , . , .

, , , required, which. , required, , - :

module Kernel
  def require_and_print(string)
    $:.each do |p|
      if File.exist? File.join(p, string)
        puts File.join(p, string)
        break
      end
    end
    require_original(string)
  end

  alias_method :require_original, :require
  alias_method :require, :require_and_print

end

require 'nokogiri'
#=>  /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rbconfig
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/pp
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/sax
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/node
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/xpath
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xslt
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/html
#    /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/css
#    /opt/local/lib/ruby1.9/1.9.1/racc/parser.rb  
+3

$gem, # (no.rb suffix) - , ...

+1

Source: https://habr.com/ru/post/1757489/


All Articles