I know it's possible to get the Ruby version (e.g. "1.9.3") via the RUBY_VERSION
constant. However, I want to know how to go about determining the exact version (e.g.: "1.9.3-p0"). The reason is that there is a bug that was not fixed in earlier versions of Ruby 1.9.3 that is working in later versions, and I want some code in a gem I'm working on to account for this.
Asked
Active
Viewed 1.3k times
42

Andrew Marshall
- 95,083
- 20
- 220
- 214

Matt Huggins
- 81,398
- 36
- 149
- 218
-
1See also https://stackoverflow.com/questions/1589751/determine-ruby-version-from-within-rails -- (not duplicate! here is Ruby, not Rails) -- but it has pure Ruby answer. – Nakilon Nov 20 '19 at 10:34
2 Answers
64
There is a RUBY_PATCHLEVEL
constant as well. So you can get your version string as
"#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"

iltempo
- 15,718
- 8
- 61
- 72
-
6Note that you should also take alternative Ruby implementations in mind (most importantly JRuby and Rubinius). Those use different versions and patch levels (but try to keep more or less compatible there with MRI) and obviously have different bugs. Generally, it is probably much safer to implement against actual capabilities rather than potentially misleading version strings – Holger Just Aug 05 '12 at 20:47
-
3@HolgerJust - I'm trying to test binary string encoding with a YAML adapter, and Psych::YAML had this bug for only some versions of 1.9.3 (see here: http://bugs.ruby-lang.org/issues/5923). Normally I would just test `defined?(Encoding)`, but that's not an option since this error would occur in some patch levels and not others. Any suggestions on a way of testing the capabilities here instead of misleading version strings? – Matt Huggins Aug 05 '12 at 22:16
-
-
2@nc. There is a `RUBY_ENGINE` constant which is set by most ruby implementations nowadays. – Holger Just Aug 28 '14 at 20:03
-
1Thanks, I'll also point out something I discovered called `RUBY_DESCRIPTION` that is a pretty verbose string. – nc. Aug 28 '14 at 21:21
-
@MattHuggins Maybe "test for the bug" here; if it passes the serialization case for the reported bug consider it Capability Enabled. Then the specific bug is being tested for (and hidden / wrapped away in some dark corner) which determines the capabilities instead of the engine or version or patch level. – user2864740 Jan 05 '15 at 21:10
13
At least in the newest Ruby (2.3.0), there is also a RUBY_DESCRIPTION
constant:
RUBY_DESCRIPTION
# => "ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]"

nitrogen
- 1,559
- 1
- 14
- 26