Determining which rubygem you're using
First of all, rubygems will load the highest version number gem, not the most recently installed. So you should be getting roo 1.3.6 here.
Other than that, I second gerrit's suggestion to look for a version constant. For instance, I just loaded rmagick and there is a constant Magick::Version. Another example is Open4::VERSION.
However not all gems have a version constant, so as a fallback you could do something hacky like:
>> require 'open4'
=> true
>> $:.detect {|dir| dir =~ /\/open4-([^\/]*)\//}
=> "/Library/Ruby/Gems/1.8/gems/open4-0.9.6/bin"
>> $1
=> "0.9.6"
How to tell which version of a gem a rails app is using
In Rails 3 and Rails 4, use bundle show
In Rails 2, rake gems
will print out what gems, dependencies, and versions are installed, frozen, etc.
How to determine which gems ship bundled with versions of Ruby
Which gems, and their specific versions, ship bundled with various versions of Ruby on Linux?
This actually does not depend so much on the Ruby version but on the Ruby implementation. For example, in Rubinius, the compiler, the parser, the Abstract Syntax Tree, the Instruction Set, and the standard library are all separate gems.
Whereas in YARV, the parser and the compiler are just parts of the monolithic VM, and the standard library is just part of the source tree.
How can I this information without having to install each version of Ruby and running
gem list
? Are there manifest files for this online anywhere?
Again, this depends heavily on the implementation.
For TruffleRuby, you can find the list of bundled gems and default gems in the file versions.json
, which currently looks like this:
{
"ruby": {
"version": "2.6.5",
"revision": 67812
},
"gems": {
"default": {
"bundler": "1.17.2",
"gem": "3.0.3",
"irb": "1.0.0",
"rake": "12.3.2",
"rdoc": "6.1.2"
},
"bundled": {
"did_you_mean": "1.3.0",
"minitest": "5.11.3",
"net-telnet": "0.2.0",
"power_assert": "1.1.3",
"rake": "12.3.2",
"test-unit": "3.2.9",
"xmlrpc": "0.3.0"
}
}
}
For YARV, you can find the list of bundled gems in the file gems/bundled_gems
in the YARV source code, which currently looks like this:
minitest 5.14.0 https://github.com/seattlerb/minitest
power_assert 1.2.0 https://github.com/ruby/power_assert
rake 13.0.1 https://github.com/ruby/rake
test-unit 3.3.5 https://github.com/test-unit/test-unit
rexml 3.2.4 https://github.com/ruby/rexml
rss 0.2.9 https://github.com/ruby/rss
I am not very familiar with the Rubinius build system, so I was not able to find a simple single location for the list of gems. I found a couple, but I don't think the list is exhaustive:
gems_list.txt
in the main Rubinius repository:
bundler-1.16.1.gem
minitest-5.11.1.gem
racc-1.4.14.gem
rake-12.3.0.gem
rb-readline-0.5.5.gem
rdoc-5.1.0.gem
The Rubinius Code repository contains tools to compile and run code on the Rubinius platform, which are used in the Rubinius Ruby implementation. The repository contains the following gems:
rubinius-ast.gemspec
:rubinius-ast
– An Abstract Syntax Tree for the Rubinius language platform.rubinius-compiler.gemspec
:rubinius-compiler
– A Bytecode compiler for the Rubinius language platform.rubinius-instructions.gemspec
:rubinius-instructions
– Support tools for Rubinius instruction set.rubinius-melbourne.gemspec
:rubinius-melbourne
– Ruby parser extracted from MRI.rubinius-processor.gemspec
:rubinius-processor
– Converts Melbourne parse tree into an AST.rubinius-toolset.gemspec
:rubinius-toolset
– A registry for Rubinius code tools.rubinius-code.gemspec
:rubinius-code
– A suite of tools for working with code on the Rubinius language platform. (A meta-gem depending on the other 6 gems,rubinius-ast
,rubinius-compiler
,rubinius-instructions
,rubinius-melbourne
,rubinius-processor
, andrubinius-toolset
.)
Of course, in addition to all of this, the ruby
package of any Linux distribution may, at the discretion of the maintainer of that package, depend on, or install any number of gems as well.
How to determine which ruby gem version is actually in use
Run
bundle list | grep unicorn
and it will show the gem version number.
How to check if a gem is installed?
General solution
To get the full list of gems that are installed:
gem list
To test for a particular gem, you can use -i
with a regex:
gem list -i "^gem_name$"
(Credit to Timo in the comments for this technique.)
Particular solution for OP
If you can't find data_mapper, it may be that the gem name is different from what you expect.
Also, if you're just doing which brew
to find brew, you aren't finding the gem called brew, you're finding the location of the brew executable. Try gem which brew
instead.
EDIT:
If you're looking for data_mapper by doing which data_mapper
, you probably won't find it. which
is a unix program for finding unix executables, and data_mapper probably doesn't have one.
Since your goal is to verify a gem is installed with the correct version, use gem list
. You can limit to the specific gem by using gem list data_mapper
.
To verify that it's installed and working, you'll have to try to require
the gem and then use it in your code.
Determine Location Of Ruby Gems
you can try
gem which rails
to fetch location for particular gem, or
echo $GEM_HOME
to fetch home dir of your gems
programmatically determine if a certain gem is installed, then install it if not
# The version requirements are optional.
# You can also specify multiple version requirements, just append more at the end
gem_name, *gem_ver_reqs = 'json', '~> 1.8.0'
gdep = Gem::Dependency.new(gem_name, *gem_ver_reqs)
# find latest that satisifies
found_gspec = gdep.matching_specs.max_by(&:version)
# instead of using Gem::Dependency, you can also do:
# Gem::Specification.find_all_by_name(gem_name, *gem_ver_reqs)
if found_gspec
puts "Requirement '#{gdep}' already satisfied by #{found_gspec.name}-#{found_gspec.version}"
else
puts "Requirement '#{gdep}' not satisfied; installing..."
# reqs_string will be in the format: "> 1.0, < 1.2"
reqs_string = gdep.requirements_list.join(', ')
# multi-arg is safer, to avoid injection attacks
system('gem', 'install', gem_name, '-v', reqs_string)
end
More recent rubygems versions provide an installer API, so instead of shelling out to the gem
command you could also use:
# using the same "gdep" variable as above
Gem.install gem_name, gdep.requirement
However, I'm not sure if Gem.install
respects your .gemrc
file.
There are a lot of useful methods for querying your installed gems (see rdocs). Some that might be helpful:
Gem::Specification.find_all_by_name
Gem::Requirement#satisfied_by?(gem_version_instance)
Gem::Specification#satisfies_requirement?(gem_dependency_instance)
Gem.loaded_specs
- hash of the gems you've actually loaded via thegem
method, or byrequire
Related Topics
Controller Can Not Detect Ajax Requests
How to Get Out of a "Hung" State in Irb
How to Save Heroku Logs to Text File
Issues Installing Ruby and Rails and Devkit on Windows 7 X64 - Fix Needed
How to Preserve Case with Http.Get
Camelcase Column in Postgresql Database in Rails (Activerecord)
Rails 4 Session.Id Occasionally Nil
How to Make a Class Whose Constructor Looks Like the Constructor of a Built-In Class
How to Avoid Truthiness in Ruby
Switch to Popup Windows in Cucumber, Capybara
In Ruby/Rails, How to Sort on a Date Value Where the Date Can Sometimes Be Null
Rails 3 Order by Count on Has_Many :Through
Use Ruby Array for a JavaScript Array in Erb. Escaping Quotes