Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 检查计算机上是否安装了库_Ruby_Gem - Fatal编程技术网

Ruby 检查计算机上是否安装了库

Ruby 检查计算机上是否安装了库,ruby,gem,Ruby,Gem,如果计算机上安装了库/gem“foo”,并且该库可用,是否有方法在不引发(和挽救)错误的情况下进行检查 可能,ruby gems或bundler的源代码中应该有一些相关的代码,但我看不出来。如果您已经安装了,那么您可以使用以下方法: 或者,您可以按以下操作: require 'rubygems' def installed?(name) if Gem::Specification.respond_to?(:find_all_by_name) Gem::Specification.f

如果计算机上安装了库/gem
“foo”
,并且该库可用,是否有方法在不引发(和挽救)错误的情况下进行检查

可能,
ruby gems
bundler
的源代码中应该有一些相关的代码,但我看不出来。

如果您已经安装了,那么您可以使用以下方法:

或者,您可以按以下操作:

require 'rubygems'

def installed?(name)
  if Gem::Specification.respond_to?(:find_all_by_name)
    Gem::Specification.find_all_by_name(name).any?
  else
    Gem.source_index.find_name(name).first
  end
end

installed?('nokogiri') # => true
installed?('foo') # => false
如果已安装,则可以使用以下方法:

或者,您可以按以下操作:

require 'rubygems'

def installed?(name)
  if Gem::Specification.respond_to?(:find_all_by_name)
    Gem::Specification.find_all_by_name(name).any?
  else
    Gem.source_index.find_name(name).first
  end
end

installed?('nokogiri') # => true
installed?('foo') # => false
我发现了相关的问题。我可以做到:

  • 检查整个加载路径:

     Gem.find_files("foo").any?
    
  • 仅检查宝石:

     Gem.find_files("foo", false).any?
    
    • 我发现了相关的问题。我可以做到:

      • 检查整个加载路径:

         Gem.find_files("foo").any?
        
      • 仅检查宝石:

         Gem.find_files("foo", false).any?
        

      gem list foo
      ,或者你是说从一个程序中吗?@matt Yes。从Ruby内部。
      gem list foo
      ,还是说从程序内部?@matt Yes。从Ruby内部。