简体   繁体   中英

How can I find out the version of a library that is packaged with Ruby, e.g. json?

Some Ruby functionality packaged with the Ruby distribution (and not needing to be installed as gems explicitly or via bundler). JSON is one example ( require 'json' ). It can be required in Ruby code but doesn't require gem installation.

Yet JSON is a gem on Github, at https://github.com/flori/json .

So how can I know which version of that gem I'm getting when I require 'json' in my code?

Alternatively many ruby gems specify their version in a constant.

You can utilize this to determine the version you are using in code or in console as well eg

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

You can find the Ruby home directory, and search below it for a directory of the appropriate name, and then inspect its version.rb file. For example, with rvm-managed rubies, I can do this (on a Posix-complaint system such as Linux or 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

I can cat ./lib/ruby/3.0.0/json/version.rb | grep 'VERSION ' cat ./lib/ruby/3.0.0/json/version.rb | grep 'VERSION ' (include the space after "VERSION"), and I get:

  VERSION         = '2.5.1'

..which is also the version of the gem in the gems directory included in the listing above.

So I can see that 2.5.1 is my JSON version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM