如何获取Ruby中已被'required'使用的文件列表? [英] How do I get a list of files that have been `required` in Ruby?

查看:115
本文介绍了如何获取Ruby中已被'required'使用的文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这纯粹是一个实验,但我想知道是否可以通过某种元编程方式在运行时获得 require 'd gem列表。例如,假设我有:

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

#不知道在这里做什么

我怎么能在运行时打印出下列内容:

 这个程序需要rubygems,sinatra,nokogiri 

解决方案

你无法完全做到这一点,因为需要一个文件可能需要其他文件,Ruby不能告诉需要的文件和别人需要的文件之间的区别。



您可以查看 $ LOADED_FEATURES 列出所有需要的东西。但是如果你想明确指定依赖关系,你应该使用 Bundler



<这是一个完全不完美的方式来猜测宝石名称和枚举一切:

  ruby​​-1.9.2-p180:001 > $ LOADED_FEATURES。 
选择{|特征| feature.include? '宝石'}。
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 ,builder,bundler,cache_stores,cancan,cdn,class,client,common,compute,connection,control,核心扩展,css,data_mapper,装饰器,依赖关系,dependency_detection,弃用,设计,摘要,dns,编码 ,加密器,引擎,错误,外部,外部,失败,法拉第,领域,雾,formatador,地理学家,haml hash,helpers,heroku_san,hmac,hooks,hoptoad_notifier,html,http,i18n,idna,importers,inflector ,插件,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 ,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签名simple_geostate_machinestats_enginestorage策略字符串tar_reader模板terremarkthor 令牌,树,树梢,微博,我们,util,vendor,version_specific,visitor,warden,xml,xml_mini ,xslt]


This is purely an experiment, but I'm wondering if it's possible to get a list of the require'd gems at runtime via some kind of metaprogramming. For example, say I have:

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

# don't know what to do here

How can I print out the following at runtime?

this app needs rubygems, sinatra, nokogiri

解决方案

You can't do this exactly, because requiring one file may require others, and Ruby can't tell the difference between the file that you required and the file that someone else required.

You can check out $LOADED_FEATURES for a list of every single thing that's been required. But you should use Bundler if you want to specify dependencies explicitly.

Here's a thoroughly imperfect way to guess at the gem names and enumerate everything:

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"] 

这篇关于如何获取Ruby中已被'required'使用的文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆