2011-08-25 20 views
28

Đây hoàn toàn là một thử nghiệm, nhưng tôi tự hỏi liệu có thể nhận được danh sách các đá quý của require 'khi chạy qua một số loại siêu lập trình hay không. Ví dụ: giả sử tôi có:Làm cách nào để có danh sách các tệp đã được `yêu cầu` trong Ruby?

require 'rubygems' 
require 'sinatra' 
require 'nokogiri' 

# don't know what to do here 

Làm cách nào để in sau đây?

this app needs rubygems, sinatra, nokogiri 
+0

Nếu $ LOADED_FEATURES không tồn tại, bạn có thể monkeypatch yêu cầu để làm những gì bạn muốn. –

+0

Câu hỏi này có câu trả lời gọn gàng bằng cách sử dụng 'Gem': http://stackoverflow.com/questions/2747990/is-there-any-way-to-tell-which-gems-and-plugins-are-loaded-at-runtime -for-a-rail – akostadinov

Trả lời

28

Bạn không thể làm điều này một cách chính xác, vì đòi hỏi phải có một tập tin có thể yêu cầu những người khác, và Ruby không thể biết sự khác biệt giữa các tập tin mà bạn cần thiết và các tập tin mà người khác yêu cầu.

Bạn có thể xem $LOADED_FEATURES để biết danh sách mọi thứ được yêu cầu. Nhưng bạn nên sử dụng Bundler nếu bạn muốn chỉ định phụ thuộc một cách rõ ràng.

Đây là một cách triệt để không hoàn hảo đoán ở những cái tên đá quý và liệt kê tất cả mọi thứ:

ruby-1.9.2-p180 :001 > $LOADED_FEATURES. 
    select { |feature| feature.include? 'gems' }. 
    map { |feature| File.dirname(feature) }. 
    map { |feature| feature.split('/').last }. 
    uniq.sort 
=> ["1.9.1", "action_dispatch", "action_pack", "action_view", "actions", "active_model", "active_record", "active_support", "addressable", "agent", "array", "aws", "builder", "bundler", "cache_stores", "cancan", "cdn", "class", "client", "common", "compute", "connection", "control", "controllers", "core", "core_ext", "core_extensions", "css", "data_mapper", "decorators", "dependencies", "dependency_detection", "deprecation", "devise", "digest", "dns", "encodings", "encryptor", "engine", "errors", "excon", "ext", "failure", "faraday", "fields", "fog", "formatador", "geographer", "haml", "hash", "helpers", "heroku_san", "hmac", "hooks", "hoptoad_notifier", "html", "http", "i18n", "idna", "importers", "inflector", "initializers", "instrumentation", "integrations", "interpolate", "interval_skip_list", "jquery-rails", "json", "kaminari", "kernel", "lib", "mail", "metric_parser", "mime", "mixins", "model_adapters", "models", "module", "mongo_mapper", "mongoid", "multibyte", "new_relic", "node", "nokogiri", "numeric", "oauth", "object", "omniauth", "orm_adapter", "package", "parser", "parsers", "plugin", "pp", "providers", "queued", "rack", "rails", "railtie", "redis", "request", "request_proxy", "resp ruby-1.9.2-p180 :008 >onse", "resque", "retriever_methods", "routing", "ruby_extensions", "ruby_flipper", "rubygems", "runtime", "samplers", "sass", "sax", "script", "scss", "selector", "sequel", "ses", "shell", "signature", "simple_geo", "state_machine", "stats_engine", "storage", "strategies", "string", "tar_reader", "template", "terremark", "thor", "tokens", "tree", "treetop", "twitter", "us", "util", "vendor", "version_specific", "visitors", "warden", "xml", "xml_mini", "xpath", "xslt"] 
+0

'$" 'là bí danh cho' $ LOADED_FEATURES' – itsnikolay

22

Dưới đây là một cách để có được tất cả các cuộc gọi đến yêu cầu. Tạo tập tin này: show_requires.rb

alias :orig_require :require 
def require s 
    print "Requires #{s}\n" if orig_require(s) 
end 

Sau đó bắt đầu ứng dụng của bạn với

ruby ​​-r show_requires.rb myapp.rb

Điều này tạo ra một cái gì đó như:

C:\code\test>ruby -r show_requires.rb test.rb 
Requires stringio 
Requires yaml/error 
Requires syck 
Requires yaml/ypath 
Requires yaml/basenode 
Requires yaml/syck 
Requires yaml/tag 
Requires yaml/stream 
Requires yaml/constants 
Requires date/format 
Requires date 
Requires yaml/rubytypes 
Requires yaml/types 
Requires yaml 
Requires etc 
Requires dl 
Requires rbreadline 
Requires readline 

Nếu bạn chỉ muốn yêu cầu cấp cao nhất, hãy thêm toàn cầu để theo dõi cấp độ lồng nhau:

$_rq_lvl = 0 
alias :orig_require :require 
def require s 
    $_rq_lvl+=1 
    print "Requires #{s}\n" if orig_require(s) and $_rq_lvl == 1 
    $_rq_lvl -=1 
end 

Sau đó, bạn nhận được:

C:\code\test>ruby -r require_test.rb test.rb 
Requires yaml 
Requires readline 
+0

Cảm ơn bài đăng này. Điều này khiến tôi gần hơn với những gì tôi đang tìm kiếm hơn câu trả lời được chấp nhận. Tuy nhiên, tôi chỉ nhận được kết quả cần thiết bằng cách vá khỉ điều này vào Kernel – jwood

Các vấn đề liên quan