How do you check the Gem Version in Ruby at Runtime?
puts Gem.loaded_specs["activesupport"].version
How to check gem version within Rails
In theory, you can access the rubygems api via the Gem
class:
Gem.latest_version_for "pry"
# => Gem::Version.new("0.9.12.6")
To get the list of current gems, you can try this.
gems = Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }
gems["pry"].version
# => Gem::Version.new("0.9.12.4")
Blatantly stolen from here.
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 do I check that a minimum Ruby version is present at runtime?
Good notice, it seems you have found possible bug, either drop a note to rails issue tracker or patch it by yourself and make a pull request.
There is small and simple Versionomy gem to compare versions correctly.
Programmatically check if gem in bundle?
Bundler is used to set up the applications gems, so you can use the Gem
API rather than Bundler
:
Gem.loaded_specs.has_key? gem_name
Bundler will have set things up so that any gems in the bundle (in the appropriate groups) have been activated (so they will have entries in loaded_specs
) and any other (non-bundle) gems will be prevented from being loaded.
List of installed gems?
The Gem command is included with Ruby 1.9+ now, and is a standard addition to Ruby pre-1.9.
require 'rubygems'
name = /^/i
dep = Gem::Dependency.new(name, Gem::Requirement.default)
specs = Gem.source_index.search(dep)
puts specs[0..5].map{ |s| "#{s.name} #{s.version}" }
# >> Platform 0.4.0
# >> abstract 1.0.0
# >> actionmailer 3.0.5
# >> actionpack 3.0.5
# >> activemodel 3.0.5
# >> activerecord 3.0.5
Here's an updated way to get a list:
require 'rubygems'
def local_gems
Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }
end
Because local_gems
relies on group_by
, it returns a hash of the gems, where the key is the gem's name, and the value is an array of the gem specifications. The value is an array of the instances of that gem that is installed, sorted by the version number.
That makes it possible to do things like:
my_local_gems = local_gems()
my_local_gems['actionmailer']
# => [Gem::Specification.new do |s|
# s.authors = ["David Heinemeier Hansson"]
# s.date = Time.utc(2013, 12, 3)
# s.dependencies = [Gem::Dependency.new("actionpack",
# Gem::Requirement.new(["= 4.0.2"]),
# :runtime),
# Gem::Dependency.new("mail",
# Gem::Requirement.new(["~> 2.5.4"]),
# :runtime)]
# s.description = "Email on Rails. Compose, deliver, receive, and test emails using the familiar controller/view pattern. First-class support for multipart email and attachments."
# s.email = "david@loudthinking.com"
# s.homepage = "http://www.rubyonrails.org"
# s.licenses = ["MIT"]
# s.name = "actionmailer"
# s.require_paths = ["lib"]
# s.required_ruby_version = Gem::Requirement.new([">= 1.9.3"])
# s.requirements = ["none"]
# s.rubygems_version = "2.0.14"
# s.specification_version = 4
# s.summary = "Email composition, delivery, and receiving framework (part of Rails)."
# s.version = Gem::Version.new("4.0.2")
# end]
And:
puts my_local_gems.map{ |name, specs|
[
name,
specs.map{ |spec| spec.version.to_s }.join(',')
].join(' ')
}
# >> actionmailer 4.0.2
...
# >> arel 4.0.1,5.0.0
...
# >> ZenTest 4.9.5
# >> zucker 13.1
The last example is similar to the gem query --local
command-line, only you have access to all the information for a particular gem's specification.
How do I check all the gems with its versions in my Rails project?
Use
bundle list
to show project related gems
To see some gems version, you can chain comands and use grep
exp.
bundle list | grep rails
will list all gems that containt word rails
, such as rails
, jquery-rails
, ...
Related Topics
How to Generate a Random Number Between a and B in Ruby
If String Is Empty Then Return Some Default Value
Why Do I Need to Work Harder to Make My Rails Application Fit into a Restful Architecture
Ruby/Rails - Models Named with Two Words (Naming Convention Issues)
Monkey-Patching VS. S.O.L.I.D. Principles
Rails: Modifying a Model Generated by Scaffolding
Why Does Ruby Open-Uri's Open Return a Stringio in My Unit Test, But a Fileio in My Controller
Activemodel::Missingattributeerror Occurs After Deploying and Then Goes Away After a While
How to Get Long Url from Short Url
How to Call Java API from Ruby 1.8 or 1.9
Is Every Relavant Calculation Performed Every Time the Page Is Loaded
Ruby - Elegantly Convert Variable to an Array If Not an Array Already
Undefined Method '>' for Nil:Nilclass <Nomethoderror>