Have a ruby script output what version of ruby is running it
How do I have my ruby script output what version of r开发者_如何学编程uby is running it?
The RUBY_VERSION
constant contains the version number of the ruby interpreter and RUBY_PATCHLEVEL
contains the patchlevel, so this:
puts RUBY_VERSION
outputs e.g. 2.2.3
, while this:
puts RUBY_PATCHLEVEL
outputs e.g. 173
. Together it can be used like this:
ruby -e 'print "ruby #{ RUBY_VERSION }p#{ RUBY_PATCHLEVEL }"'
to output e.g. ruby 2.2.3p173
For reference, here's how variables and constants work, along with a list of Ruby's built-in variables and constants: Ruby Programming/Syntax/Variables and Constants and Pre-defined Variables.
You can get a list of all the global constants here, including RUBY_VERSION
and friends, in the official Ruby language documentation.
For the bonus round, this will tell you some more useful info about your Ruby environment using RbConfig:
require 'rbconfig'
puts Config::CONFIG.sort_by{ |n,v| n.downcase }.map{ |n,v| "#{n} => '#{v}'" }
精彩评论