Forum: Ruby How can I get the Ruby version from within a Ruby script?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B17d29d56cd9d4ccd91056eff4a355e3?d=identicon&s=25 Paul (Guest)
on 2007-07-16 17:37
(Received via mailing list)
I want to log the current Ruby version to a log file along with the
script version, but I don't know how to get the Ruby version from
within a running script.

I tried:
   puts system("ruby -v")

but that only returns "true" from within a script.  (I see the correct
output when I run it in IRB, but the script doesn't capture that.)

I also tried :
   puts Config::CONFIG["ruby_version"]

but that returns "1.8" and I would like "1.8.x".

Any suggestions?  Please let me know.  Thanks.
B4b39bb32dcaa75317b5ccea71080c36?d=identicon&s=25 Phil Meier (Guest)
on 2007-07-16 17:53
(Received via mailing list)
Paul schrieb:
> I want to log the current Ruby version to a log file along with the
> script version, but I don't know how to get the Ruby version from
> within a running script.

Just use constant RUBY_VERSION.

puts RUBY_VERSION
=> 1.8.6
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-07-16 18:02
(Received via mailing list)
On Jul 16, 2007, at 9:55 AM, Paul wrote:

> I also tried :
>    puts Config::CONFIG["ruby_version"]
>
> but that returns "1.8" and I would like "1.8.x".
>
> Any suggestions?  Please let me know.  Thanks.
>
>
the constant VERSION will return the version number (only)
puts "Ruby version #{VERSION}"
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2007-07-16 18:04
(Received via mailing list)
Alle lunedì 16 luglio 2007, Paul ha scritto:
> I also tried :
>    puts Config::CONFIG["ruby_version"]
>
> but that returns "1.8" and I would like "1.8.x".
>
> Any suggestions?  Please let me know.  Thanks.

You can try

  puts(RUBY_VERSION)

By the way, if you need to get the output of an external command, you
need to
use `cmd`, instead of system("cmd").

Stefano
D0338c0de4cb3c5c17300396159933d1?d=identicon&s=25 Axel Etzold (Guest)
on 2007-07-16 18:06
(Received via mailing list)
Paul,

this works for me:

result= `ruby -v`
p 'my result'
p result

Best regards,

Axel
15575d911f9ad5e2fc3547f5f05a185f?d=identicon&s=25 Paul Knight (Guest)
on 2007-07-16 19:18
(Received via mailing list)
On Jul 16, 2007, at 8:57 AM, Axel Etzold wrote:

> this works for me:
>
> result= `ruby -v`
> p 'my result'
> p result

Unfortunately, this grabs the version of some Ruby, not necessarily
the one currently running. The RUBY_VERSION constant is what you
probably want.

Paul Knight (A different Paul than the OP)
B17d29d56cd9d4ccd91056eff4a355e3?d=identicon&s=25 Paul (Guest)
on 2007-07-16 19:21
(Received via mailing list)
On Jul 16, 11:13 am, Phil Meier wrote:
>
> Just use constant RUBY_VERSION.
>
> puts RUBY_VERSION
> => 1.8.6

That's perfect!  Thank you.

I don't know why Google didn't turn that up anywhere.  I even checked
the Pickaxe book but I didn't find it under "V" for 'version'.

Cheers!
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-07-16 19:57
(Received via mailing list)
On 16.07.2007 19:16, Paul wrote:
> On Jul 16, 11:13 am, Phil Meier wrote:
>> Just use constant RUBY_VERSION.
>>
>> puts RUBY_VERSION
>> => 1.8.6
>
> That's perfect!  Thank you.
>
> I don't know why Google didn't turn that up anywhere.  I even checked
> the Pickaxe book but I didn't find it under "V" for 'version'.

Since the version is a good candidate for a constant you can try this:

Robert@Babelfish2 ~
$ ruby -e 'Object.constants.sort.each {|c| cv=Object.const_get(c); print
c, "=", cv, "\n" unless Module === cv}'
ARGF=ARGF
ARGV=
ENV=ENV
FALSE=false
NIL=nil
PLATFORM=i386-cygwin
RELEASE_DATE=2007-03-13
RUBY_PATCHLEVEL=0
RUBY_PLATFORM=i386-cygwin
RUBY_RELEASE_DATE=2007-03-13
RUBY_VERSION=1.8.6
STDERR=#<IO:0x100362d0>
STDIN=#<IO:0x100362f8>
STDOUT=#<IO:0x100362e4>
TOPLEVEL_BINDING=#<Binding:0x100302a4>
TRUE=true
VERSION=1.8.6

Robert@Babelfish2 ~
$

Or just use IRB.

Kind regards

  robert
This topic is locked and can not be replied to.