Which Ruby is in use?

H

Hassan Schroeder

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,
 
H

Hassan Schroeder

On Tue, Jul 27, 2010 at 2:20 PM, Joseph E. Savard

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?
 
J

Joel VanderWerf

Hassan said:
On Tue, Jul 27, 2010 at 2:20 PM, Joseph E. Savard

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?
=> "/usr/local/bin/ruby"
 
H

Hassan Schroeder

Maybe this will work?

=> "/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 :)
 
J

Joel VanderWerf

Hassan said:
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 :)

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"
 
H

Hassan Schroeder

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

--=20
Hassan Schroeder ------------------------ (e-mail address removed)
twitter: @hassan
 
J

Joel VanderWerf

Hassan said:
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"
 
H

Hassan Schroeder

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

Fabian Marin

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 :p
 
H

Hassan Schroeder

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 :p

Thanks, but
$ irb=> "/bin/sh\n"

isn't what I was looking for :)
 
E

Edward Middleton

Thanks, but
$ irb
=> "/bin/sh\n"

isn't what I was looking for :)

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
 
F

Fabian Marin

Edward said:
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.
 
C

Caleb Clausen

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

Roger Pack

Hassan said:
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:
=> "c:/installs/ruby192-rc1/bin/ruby.exe"

Or
require 'rubygems'

Gem.ruby

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

Hassan Schroeder

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

Thanks everyone for the suggestions!
 
R

Roger Pack

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

ENV['_']
doesn't seem to work for me on windows at all:
=> nil

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

Caleb Clausen

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

ENV['_']
doesn't seem to work for me on windows at all:
=> nil

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

I just did a quick check, and I'm not seeing anywhere in the ruby
source where it sets an envvar named _. Perhaps this envvar is a
feature of bash or linux or something other than ruby.
 
J

Joel VanderWerf

Caleb said:
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 :)
ENV['_'] doesn't seem to work for me on windows at all:
=> nil

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

I just did a quick check, and I'm not seeing anywhere in the ruby
source where it sets an envvar named _. Perhaps this envvar is a
feature of bash or linux or something other than ruby.

Yeah, I think it's just a shell feature:

$ ruby -e 'p ENV["_"]'
"/usr/local/bin/ruby"
$ env ruby -e 'p ENV["_"]'
"/usr/bin/env"

That's the behavior in both zsh and bash.

Found this in some bash docs:
 
H

Hassan Schroeder

That's the behavior in both zsh and bash.

Ah, good detectorizing :) and that explains the JRuby "issue" -- even
though $JRUBY_HOME/bin/jruby is a bash script, it contains this line:

JRUBY_SHELL=/bin/sh

so apparently no '_' environment variable will be forthcoming.

Ya learn something every day!
 

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

Forum statistics

Threads
474,141
Messages
2,570,814
Members
47,360
Latest member
kathdev

Latest Threads

Top