如何获取 Ruby 中“必需"的文件列表? [英] How do I get a list of files that have been `required` in Ruby?

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

问题描述

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

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

推荐答案

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

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.

您可以查看 $LOADED_FEATURES 以获取所需的每项内容的列表.但是如果你想明确指定依赖关系,你应该使用 Bundler.

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 中“必需"的文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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