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

P

Paul

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.
 
P

Phil Meier

Paul said:
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
 
J

John Joyce

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.
the constant VERSION will return the version number (only)
puts "Ruby version #{VERSION}"
 
S

Stefano Crocco

Alle luned=EC 16 luglio 2007, Paul ha scritto:
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.

You can try

puts(RUBY_VERSION)

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

Stefano
 
A

Axel Etzold

Paul,

this works for me:

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

Best regards,

Axel
 
P

Paul Knight

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)
 
P

Paul

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!
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top