如何找到用 Ruby 打包的库的版本,例如json?

问题描述 投票:0回答:3

一些 Ruby 功能与 Ruby 发行版一起打包(不需要显式或通过捆绑程序安装为 gem)。 JSON 就是一个例子 (

require 'json'
)。 Ruby 代码中可能需要它,但不需要安装 gem。

然而,JSON 是 Github 上的瑰宝,位于 https://github.com/flori/json

那么当我在代码中需要“json”时,我如何知道我得到的是哪个版本的 gem?

json ruby rubygems version
3个回答
1
投票

许多 ruby gem 在常量中指定它们的版本。

您可以利用它来确定您在代码或控制台中使用的版本,例如

require 'json'
JSON.constants.grep(/VERSION/)
#=>[:VERSION, :VERSION_ARRAY, :VERSION_MAJOR, :VERSION_MINOR, :VERSION_BUILD]
JSON::VERSION
#=> 2.5.1
require 'bundler'
Bundler.constants.grep(/VERSION/)
#=> [:VERSION]
Bundler::VERSION
#=> 2.2.3

0
投票

您可以找到 Ruby 主目录,并在其下面搜索相应名称的目录,然后检查其

version.rb
文件。例如,使用 rvm 管理的 rubies,我可以执行此操作(在符合 Posix 的系统上,例如 Linux 或 Mac OS):

$ cd $(which ruby)/../..; pwd
/Users/keith.bennett/.rvm/rubies/ruby-3.0.1

$find . -type d -name '*json*'
./lib/ruby/3.0.0/psych/json
./lib/ruby/3.0.0/json
./lib/ruby/3.0.0/rdoc/generator/template/json_index
./lib/ruby/3.0.0/x86_64-darwin19/json
./lib/ruby/gems/3.0.0/gems/json-2.5.1
./lib/ruby/gems/3.0.0/gems/rbs-1.0.4/stdlib/json

我可以

cat ./lib/ruby/3.0.0/json/version.rb | grep 'VERSION '
(包括“VERSION”后面的空格),我得到:

  VERSION         = '2.5.1'

..这也是上面列表中包含的

gems
目录中 gem 的版本。

所以我可以看到 2.5.1 是我的 JSON 版本。


0
投票

对于那些寻找甚至适用于不包含版本信息常量的 gem 或文件的解决方案的人:

require 'json'
$LOADED_FEATURES.select { |x| x.match? 'json' }
© www.soinside.com 2019 - 2024. All rights reserved.