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,

···

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

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?

···

From: Hassan Schroeder <hassan.schroeder@gmail.com>
Reply-To: <ruby-talk@ruby-lang.org>
Date: Wed, 28 Jul 2010 05:26:42 +0900
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Subject: 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,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

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 (C) 1993-2010 Yukihiro Matsumoto"
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.

From: Hassan Schroeder <hassan.schroeder@gmail.com>
Reply-To: <ruby-talk@ruby-lang.org>
Date: Wed, 28 Jul 2010 05:26:42 +0900
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Subject: 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,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

RUBY_COPYRIGHT
RUBY_DESCRIPTION
RUBY_ENGINE String
RUBY_PATCHLEVEL
RUBY_PLATFORM

Hassan Schroeder 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

···

--
Posted via http://www.ruby-forum.com/\.

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?

···

On Tue, Jul 27, 2010 at 2:20 PM, Joseph E. Savard <joseph.savard@sabre-holdings.com> wrote:

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

Hassan Schroeder 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 2:20 PM, Joseph E. Savard > <joseph.savard@sabre-holdings.com> wrote:

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 3:10 PM, Joel VanderWerf <joelvanderwerf@gmail.com> wrote:

Maybe this will work?

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

=> "/usr/local/bin/ruby"

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

Hassan Schroeder 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(TM) 64-Bit Server VM 1.6.0_20) [amd64-java]
"/usr/local/jruby/bin/jruby"

···

On Tue, Jul 27, 2010 at 3:10 PM, Joel VanderWerf > <joelvanderwerf@gmail.com> wrote:

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"])

From: Hassan Schroeder <hassan.schroeder@gmail.com>
Reply-To: <ruby-talk@ruby-lang.org>
Date: Wed, 28 Jul 2010 07:30:25 +0900
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Subject: Re: Which Ruby is in use?

On Tue, Jul 27, 2010 at 3:10 PM, Joel VanderWerf > <joelvanderwerf@gmail.com> 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:

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

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:~$

···

On Tue, Jul 27, 2010 at 3:52 PM, Joel VanderWerf <joelvanderwerf@gmail.com> 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?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

Hassan Schroeder wrote:

···

On Tue, Jul 27, 2010 at 3:52 PM, Joel VanderWerf > <joelvanderwerf@gmail.com> 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:~$

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"

You forgot to require "rbconfig"

ruby -v -rrbconfig

···

From: Hassan Schroeder <hassan.schroeder@gmail.com>
Reply-To: <ruby-talk@ruby-lang.org>
Date: Wed, 28 Jul 2010 08:22:42 +0900
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Subject: Re: Which Ruby is in use?

On Tue, Jul 27, 2010 at 3:52 PM, Joel VanderWerf > <joelvanderwerf@gmail.com> 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:~$

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

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.

···

On Tue, Jul 27, 2010 at 4:45 PM, Joel VanderWerf <joelvanderwerf@gmail.com> wrote:

You must require 'rbconfig':

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

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:

···

--
Posted via http://www.ruby-forum.com/.

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 7:03 PM, Fabian Marin <fmg134s@yahoo.com> wrote:

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

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

···

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

On Tue, Jul 27, 2010 at 7:03 PM, Fabian Marin <fmg134s@yahoo.com> 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:

Edward Middleton wrote:

···

On 07/28/2010 11:20 AM, Hassan Schroeder 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.
--
Posted via http://www.ruby-forum.com/\.

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.

···

On 7/27/10, Fabian Marin <fmg134s@yahoo.com> wrote:

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

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 Tue, Jul 27, 2010 at 11:41 PM, Caleb Clausen <vikkous@gmail.com> 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.

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

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

···

--
Posted via http://www.ruby-forum.com/\.