Which Ruby is in use?

Is there a way to tell from within a program which executable is being
used – which executable, not the version – to run it?

Seems like there should be, but I’m striking out…

TIA,

The best way is to check your path or your #! Line…

From IRB:

ruby-1.9.2-head > RUBY_VERSION
=> “1.9.2”
ruby-1.9.2-head > RUBY_PLATFORM
=> “i386-darwin9.8.0”
ruby-1.9.2-head > RUBY_RELEASE_DATE
=> “2010-07-14”
ruby-1.9.2-head > RUBY_PATCHLEVEL
=> -1
ruby-1.9.2-head >

HERE’s the money shot!

FROM A RUBY PROGRAM: [args.rb]
puts “The name of the progrma laucnhed is #{$0} OR #{$PROGRAM_NAME}”
ARGV.each do|a|

puts “Argument: #{a}”
end

puts “And finally the ruby executable #{ENV[’_’]}”

MANISH:jes [07-27 16:17] 0 529:29 (14.35 Mb) • ~
! irb args.rb a s d
ruby-1.9.2-head >
ruby-1.9.2-head > puts “The name of the progrma laucnhed is #{$0} OR
#{$PROGRAM_NAME}”
The name of the progrma laucnhed is args.rb OR args.rb
=> nil
ruby-1.9.2-head > ARGV.each do|a|
ruby-1.9.2-head >
ruby-1.9.2-head > puts “Argument: #{a}”
ruby-1.9.2-head ?> end
Argument: a
Argument: s
Argument: d
=> [“a”, “s”, “d”]
ruby-1.9.2-head >
ruby-1.9.2-head > puts “And finally the ruby executable #{ENV[’_’]}”
And finally the ruby executable
/Users/jes/.rvm/rubies/ruby-1.9.2-head/bin/irb
=> nil
ruby-1.9.2-head >

OR RBCONFIG if you like:
ruby-1.9.2-head > require “rbconfig”
=> false
ruby-1.9.2-head > puts File.join(Config::CONFIG[“bindir”],
Config::CONFIG[“ruby_install_name”])
/Users/jes/.rvm/rubies/ruby-1.9.2-head/bin/ruby
=> nil
ruby-1.9.2-head >

Make sense?

In 1.9:
RUBY_COPYRIGHT
RUBY_DESCRIPTION String Version number 1.9 & interpreter arch.
RUBY_ENGINE String The name of the Ruby interpreter
RUBY_PATCHLEVEL
RUBY_PLATFORM

Results in irb:
ruby-1.9.2-head > RUBY_COPYRIGHT
=> “ruby - Copyright © 1993-2010 Yukihiro M.”
ruby-1.9.2-head > RUBY_DESCRIPTION
=> “ruby 1.9.2dev (2010-07-14 revision 28640) [i386-darwin9.8.0]”
ruby-1.9.2-head > RUBY_ENGINE
=> “ruby”
ruby-1.9.2-head > RUBY_PATCHLEVEL
=> -1
ruby-1.9.2-head > RUBY_PLATFORM
=> “i386-darwin9.8.0”
ruby-1.9.2-head >

Lots of ways to skin the cat.

TIA,

Hassan S. ------------------------ [email protected]
twitter: @hassan

RUBY_COPYRIGHT
RUBY_DESCRIPTION
RUBY_ENGINE String
RUBY_PATCHLEVEL
RUBY_PLATFORM

Hassan S. wrote:

On Tue, Jul 27, 2010 at 2:20 PM, Joseph E. Savard
[email protected] wrote:

As I said, I’m not interested in the version, just the path to the executable.

puts “And finally the ruby executable #{ENV[’_’]}”

Doesn’t appear to work in JRuby. Do you know if it works in anything
else besides MRI?

Maybe this will work?

File.join *Config::CONFIG.values_at(“bindir”, “ruby_install_name”)
=> “/usr/local/bin/ruby”

On Tue, Jul 27, 2010 at 3:10 PM, Joel VanderWerf
[email protected] wrote:

Maybe this will work?

File.join *Config::CONFIG.values_at(“bindir”, “ruby_install_name”)
=> “/usr/local/bin/ruby”

Works in a Rails console, but not a Ruby program. But thanks, now
that I consider it I should be able to work with that :slight_smile:

On Tue, Jul 27, 2010 at 2:20 PM, Joseph E. Savard
[email protected] wrote:

As I said, I’m not interested in the version, just the path to the
executable.

puts “And finally the ruby executable #{ENV[’_’]}”

Doesn’t appear to work in JRuby. Do you know if it works in anything
else besides MRI?

Hassan S. wrote:

On Tue, Jul 27, 2010 at 3:10 PM, Joel VanderWerf
[email protected] wrote:

Maybe this will work?

File.join *Config::CONFIG.values_at(“bindir”, “ruby_install_name”)
=> “/usr/local/bin/ruby”

Works in a Rails console, but not a Ruby program. But thanks, now
that I consider it I should be able to work with that :slight_smile:

Hm, that’s surprising. I tested it in ruby and jruby programs. What’s
the problem?

$ ruby -v -rrbconfig -e ‘p File.join(*Config::CONFIG.values_at(“bindir”,
“ruby_install_name”))’
ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]
“/usr/local/bin/ruby”

$ ruby19 -v -rrbconfig -e ‘p
File.join(*Config::CONFIG.values_at(“bindir”, “ruby_install_name”))’
ruby 1.9.2dev (2010-07-02 revision 28524) [x86_64-linux]
“/usr/local/bin/ruby19”

$ jruby -v -rrbconfig -e ‘p
File.join(*Config::CONFIG.values_at(“bindir”, “ruby_install_name”))’
jruby 1.5.0 (ruby 1.8.7 patchlevel 249) (2010-05-12 6769999) (Java
HotSpot™ 64-Bit Server VM 1.6.0_20) [amd64-java]
“/usr/local/jruby/bin/jruby”

Hassan S. wrote:

ripple:~$ cat foo.rb
puts File.join *Config::CONFIG.values_at(“bindir”, “ruby_install_name”)
ripple:~$ ruby foo.rb
foo.rb:1: uninitialized constant Config (NameError)
ripple:~$ jruby foo.rb
foo.rb:1: uninitialized constant Config (NameError)
ripple:~$

You must require ‘rbconfig’:

$ ruby -v -rrbconfig -e ‘p File.join(*Config::CONFIG.values_at(“bindir”,
“ruby_install_name”))’
ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]
“/usr/local/bin/ruby”

On Tue, Jul 27, 2010 at 3:52 PM, Joel VanderWerf
[email protected] wrote:

File.join *Config::CONFIG.values_at(“bindir”, “ruby_install_name”)

=> “/usr/local/bin/ruby”

Works in a Rails console, but not a Ruby program. But thanks, now
that I consider it I should be able to work with that :slight_smile:

Hm, that’s surprising. I tested it in ruby and jruby programs. What’s the
problem?

ripple:~$ cat foo.rb
puts File.join *Config::CONFIG.values_at(“bindir”, “ruby_install_name”)
ripple:~$ ruby foo.rb
foo.rb:1: uninitialized constant Config (NameError)
ripple:~$ jruby foo.rb
foo.rb:1: uninitialized constant Config (NameError)
ripple:~$

hope I did not misunderstand the question, but would this work??

%x{which $0}

It seems too easy to work so I probably missed something :stuck_out_tongue:

On Tue, Jul 27, 2010 at 7:03 PM, Fabian M. [email protected] wrote:

hope I did not misunderstand the question, but would this work??

%x{which $0}

It seems too easy to work so I probably missed something :stuck_out_tongue:

Thanks, but
$ irb

%x{which $0}
=> “/bin/sh\n”

isn’t what I was looking for :slight_smile:

On Tue, Jul 27, 2010 at 4:45 PM, Joel VanderWerf
[email protected] wrote:

You must require ‘rbconfig’:

Yes, I see that – my first thought was that that’s not going to be
practical
in this case, but thanks, I’ll play around with it.

Edward Middleton wrote:

On 07/28/2010 11:20 AM, Hassan S. wrote:

=> “/bin/sh\n”

isn’t what I was looking for :slight_smile:

Thats because the $0 is being evaluated by the shell not ruby. What you
actually want is %x{which #{$0}} which will give “which irb” to the
shell. If the PATH environment is the same this should give you the
full path.

Edward

Yeah I missed that. I could not execute/test the code I suggested
because I’m using Win XP right now, sigh…

Edward, you’re right. Once $0 is evaluated by Ruby that should work in
a Unix environment.

On 07/28/2010 11:20 AM, Hassan S. wrote:

=> “/bin/sh\n”

isn’t what I was looking for :slight_smile:

Thats because the $0 is being evaluated by the shell not ruby. What you
actually want is %x{which #{$0}} which will give “which irb” to the
shell. If the PATH environment is the same this should give you the
full path.

Edward

Hassan S. wrote:

Is there a way to tell from within a program which executable is being
used – which executable, not the version – to run it?

os gem:

require ‘os’
OS.ruby_bin
=> “c:/installs/ruby192-rc1/bin/ruby.exe”

Or
require ‘rubygems’

Gem.ruby

I believe.
Or apparently ENV[’_’] thanks Caleb.
-r
-r

Maybe
Well it works in an ruby prog. It maybe it truly is MRI issue.

Seee if I can fire up j/iron ruby

args.rb below:

require “rbconfig”

puts “The name of the progrma laucnhed is #{$0} OR #{$PROGRAM_NAME}”
ARGV.each do|a|

puts “Argument: #{a}”
end

puts “And finally the ruby executable #{ENV[’_’]}”

puts
File.join(Config::CONFIG[“bindir”],Config::CONFIG[“ruby_install_name”])

On Tue, Jul 27, 2010 at 11:41 PM, Caleb C. [email protected]
wrote:

Hassan, what is you objection to using rbconfig.rb as Joel suggests?
AFAIK, that’s the best (only real) solution to this particular
problem.

Yeah, that’s the pick of the litter. I was looking for the minimal
solution
and it seemed unDRY to have to require that everywhere I might need
the information, but – undercaffeinated premature optimization aside –
since my real current use case only requires patching one (Rails app)
plugin it’s fine.

I haven’t checked yet whether there’s a bug filed against JRuby for
the lack of support for ENV[’_’], which obviously wins the minimalist
contest :slight_smile:

Thanks everyone for the suggestions!

On 7/27/10, Fabian M. [email protected] wrote:

Edward, you’re right. Once $0 is evaluated by Ruby that should work in
a Unix environment.

But watch:

$ ruby -e ‘p $0’
“-e”

That doesn’t seem to helpful. Hassan seemed to want the path to the
ruby interpreter, not the path to the ruby script it’s executing.

Hassan, what is you objection to using rbconfig.rb as Joel suggests?
AFAIK, that’s the best (only real) solution to this particular
problem.

You forgot to require “rbconfig”

ruby -v -rrbconfig

I haven’t checked yet whether there’s a bug filed against JRuby for
the lack of support for ENV[’_’], which obviously wins the minimalist
contest :slight_smile:

ENV[’_’]
doesn’t seem to work for me on windows at all:

ENV[’_’]
=> nil

Thought it might still be a bug in jruby that it not have one under
linux, I’m not entirely sure.
-r

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs