你如何检查是否已加载库/ ruby​​-gem?

时间:2021-12-10 07:33:17

In ruby code, how would I check what external libraries are loaded? For example,

在ruby代码中,我如何检查加载了哪些外部库?例如,

require 'some-library'
if is-loaded?('some-library')
  puts "this will run"
end

or

要么

# require 'some-library' Don't load it in here
if is-loaded?('some-library')
  puts "this will not run"
end

Is there a way to do this?

有没有办法做到这一点?

Note on why I need this: I'm working on boom, and on windows, it will try to include 'Win32/Console/ANSI', to enable ANSI color codes like \e[36m. What I'm trying to do is if the system is windows and 'Win32/Console/ANSI' is not loaded, it would append the color codes, so the color codes are not outputted. Here is the file.

请注意我为什么需要这个:我正在研究繁荣,在Windows上,它将尝试包含'Win32 / Console / ANSI',以启用ANSI颜色代码,如\ e [36m。我要做的是,如果系统是Windows并且未加载“Win32 / Console / ANSI”,它将附加颜色代码,因此不输出颜色代码。这是文件。

5 个解决方案

#1


22  

Most libraries will typically define a top-level constant. The usual thing to do is to check whether that constant is defined.

大多数库通常会定义*常量。通常要做的是检查是否定义了常量。

> defined?(CSV)
#=> nil

> require "csv"
#=> true

> defined?(CSV)
#=> "constant"

> puts "loaded!" if defined?(CSV)
loaded!
#=> nil

#2


10  

require will throw a LoadError if it can't find the library you are trying to load. So you can check it like this

如果找不到您尝试加载的库,则require将抛出LoadError。所以你可以这样检查一下

begin
  require 'some-library'
  puts 'This will run.'
rescue LoadError
  puts 'This will not run'
  # error handling code here
end

#3


6  

Require Library Unless Already Loaded

For simplicity, here's how you load a library unless it's already loaded:

为简单起见,这里是如何加载库,除非它已经加载:

require 'RMagick' unless defined?(Magick)

#4


4  

If you want to safely try requiring a gem/library that may or may not be available, use something like this:

如果您想安全地尝试要求可能有或没有可用的gem /库,请使用以下内容:

begin
  require 'securerandom'
rescue LoadError
  # We just won't get securerandom
end

This works even if the gem in question has already been required. In that scenario the require statement will do nothing and the rescue block will never execute.

即使已经要求有问题的宝石,这也有效。在那种情况下,require语句将不执行任何操作,并且救援块将永远不会执行。

If you are just interested in whether or not a gem/library has already been loaded, check to see if one of its constants is present. I do something like this to dynamically load additional functionality if ActiveSupport is loaded:

如果您只对是否已加载gem /库感兴趣,请检查其中是否存在其中一个常量。如果加载了ActiveSupport,我会这样做以动态加载其他功能:

if defined?(ActiveSupport)
  require "active_support/cache/redis_store"
end

You can also use the opposite to load a compatibility layer if the gem/library is NOT present. For example, I use some Hash methods that don't exist in Ruby's core Hash implementation, but are added by ActiveSupport. So, I define those methods when my gem runs in an environment where ActiveSupport doesn't exist.

如果gem / library不存在,您也可以使用相反的方法加载兼容层。例如,我使用一些在Ruby的核心Hash实现中不存在的Hash方法,但是由ActiveSupport添加。因此,当我的gem在不存在ActiveSupport的环境中运行时,我定义了这些方法。

require 'core_ext/hash' unless defined?(ActiveSupport)

#5


0  

try this :

尝试这个 :

def loaded?(name)
  r = Regexp.new("#{name}.rb$")
  $LOADED_FEATURES.select{|t| t.match(r) }.any?
end

Be sure of the name of your module (search here $LOADED_FEATURES).

确保模块的名称(在此处搜索$ LOADED_FEATURES)。

#1


22  

Most libraries will typically define a top-level constant. The usual thing to do is to check whether that constant is defined.

大多数库通常会定义*常量。通常要做的是检查是否定义了常量。

> defined?(CSV)
#=> nil

> require "csv"
#=> true

> defined?(CSV)
#=> "constant"

> puts "loaded!" if defined?(CSV)
loaded!
#=> nil

#2


10  

require will throw a LoadError if it can't find the library you are trying to load. So you can check it like this

如果找不到您尝试加载的库,则require将抛出LoadError。所以你可以这样检查一下

begin
  require 'some-library'
  puts 'This will run.'
rescue LoadError
  puts 'This will not run'
  # error handling code here
end

#3


6  

Require Library Unless Already Loaded

For simplicity, here's how you load a library unless it's already loaded:

为简单起见,这里是如何加载库,除非它已经加载:

require 'RMagick' unless defined?(Magick)

#4


4  

If you want to safely try requiring a gem/library that may or may not be available, use something like this:

如果您想安全地尝试要求可能有或没有可用的gem /库,请使用以下内容:

begin
  require 'securerandom'
rescue LoadError
  # We just won't get securerandom
end

This works even if the gem in question has already been required. In that scenario the require statement will do nothing and the rescue block will never execute.

即使已经要求有问题的宝石,这也有效。在那种情况下,require语句将不执行任何操作,并且救援块将永远不会执行。

If you are just interested in whether or not a gem/library has already been loaded, check to see if one of its constants is present. I do something like this to dynamically load additional functionality if ActiveSupport is loaded:

如果您只对是否已加载gem /库感兴趣,请检查其中是否存在其中一个常量。如果加载了ActiveSupport,我会这样做以动态加载其他功能:

if defined?(ActiveSupport)
  require "active_support/cache/redis_store"
end

You can also use the opposite to load a compatibility layer if the gem/library is NOT present. For example, I use some Hash methods that don't exist in Ruby's core Hash implementation, but are added by ActiveSupport. So, I define those methods when my gem runs in an environment where ActiveSupport doesn't exist.

如果gem / library不存在,您也可以使用相反的方法加载兼容层。例如,我使用一些在Ruby的核心Hash实现中不存在的Hash方法,但是由ActiveSupport添加。因此,当我的gem在不存在ActiveSupport的环境中运行时,我定义了这些方法。

require 'core_ext/hash' unless defined?(ActiveSupport)

#5


0  

try this :

尝试这个 :

def loaded?(name)
  r = Regexp.new("#{name}.rb$")
  $LOADED_FEATURES.select{|t| t.match(r) }.any?
end

Be sure of the name of your module (search here $LOADED_FEATURES).

确保模块的名称(在此处搜索$ LOADED_FEATURES)。