Programmatically Getting Full Ruby Version

Programmatically getting FULL Ruby version?

There is a RUBY_PATCHLEVEL constant as well. So you can get your version string as

"#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"

Programmatically getting FULL Ruby version?

There is a RUBY_PATCHLEVEL constant as well. So you can get your version string as

"#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"

Determine ruby version from within Rails

Use this global constant:

RUBY_VERSION

Other relevant global constants include:

RUBY_PATCHLEVEL
RUBY_PLATFORM
RUBY_RELEASE_DATE

Usage example via irb session:

irb(main):001:0> RUBY_VERSION
=> "1.8.7"

How can I programmatically get the ABI compatibility level (minor version) of my Ruby installation?

There is no way I know of to get the ABI compatibility level programmatically.

However, given your comments, it seems that you are not actually caring about the ABI compatibility level but about the RubyGems Gem installation path. Since this path can be configured by the user in a configuration file, environment variable or command line argument, I think it would make much more sense to ask RubyGems for the Gem installation path using the Gem::dir method instead of asking Ruby for the ABI compatibility level:

Gem.dir
#=> "/usr/local/lib/ruby/gems/2.7.0"

How to programmatically get Ruby's standard library (or system gem) path?

The Gem lib has a constant: Gem::RUBYGEMS_DIR

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.

Verify version of a gem with bundler from inside Ruby

A way to avoid external execution:

For bundler 1.2.x

require 'bundler/cli'

# intercepting $stdout into a StringIO
old_stdout, $stdout = $stdout, StringIO.new

# running the same code run in the 'bundler outdated' utility
Bundler::CLI.new.outdated('rails')

# storing the output
output = $stdout.string

# restoring $stdout
$stdout = old_stdout

For bundler 1.3.x

require 'bundler/cli'
require 'bundler/friendly_errors'

# let's cheat the CLI class with fake exit method
module Bundler
class CLI
desc 'exit', 'fake exit' # this is required by Thor
def exit(*); end # simply do nothing
end
end

# intercepting $stdout into a StringIO
old_stdout, $stdout = $stdout, StringIO.new

# running the same code run in the 'bundler outdated' utility
Bundler.with_friendly_errors { Bundler::CLI.start(['outdated', 'rails']) }

# storing the output
output = $stdout.string

# restoring $stdout
$stdout = old_stdout

Should I check in `.ruby-gemset` and/or `.ruby-version`?

For standard projects

Check in .ruby-version if your project depends on a ruby like ruby-2.0.0.
Check in .ruby-gemset only if your team agreed on it.
Add .rvmrc to .gitignore so anyone can override .ruby-* files settings with .rvmrc.

For gems

Check in .ruby-version with ruby-1.8.7 only if your project still targets ruby 1.8.7, otherwise check it in only if your gem requires it.
Do not check in .ruby-gemset.



Related Topics



Leave a reply



Submit