How to Find the Ruby Interpreter

How do I tell which Ruby interpreter I'm using?

Nowadays, all mainstream Ruby implementations set the RUBY_ENGINE pseudo-constant. The values for the various implementations which I can remember off the top of my head are:

  • Rubinius: rbx
  • JRuby: jruby
  • TruffleRuby: truffleruby
  • Opal: opal
  • MRuby: mruby
  • YARV: confusingly, ruby
  • MRI: even more confusingly, also ruby
  • MagLev: maglev
  • IronRuby: ironruby
  • MacRuby: macruby
  • Topaz: topaz

How do I find the ruby interpreter?

These days (1.9+) you can use built-in methods (which are supposed to work with Jruby, etc.) like this:

RbConfig.ruby
or
Gem.ruby

$ irb --simple-prompt
>> RbConfig.ruby
=> "C:/installs/Ruby193/bin/ruby.exe"
>> Gem.ruby
=> "C:/installs/Ruby193/bin/ruby.exe"

Where is the Ruby interpreter located?

You can run which ruby to find out where the ruby is that will execute if you type ruby in the Terminal.

If you want to find more information out about the executable, you can run:

$ ls -l $(which ruby)
lrwxr-xr-x 1 root wheel 76 Nov 8 12:56 /usr/bin/ruby -> ../../System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby

That is, execute which ruby, and pass the results of that into ls -l, which will show you that it's actually a symlink to the binary in the Ruby framework. You can also use file to find out what kind of file it is:

$ file $(which ruby)
/usr/bin/ruby: Mach-O universal binary with 2 architectures
/usr/bin/ruby (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/ruby (for architecture i386): Mach-O executable i386

If you want to make sure you execute the ruby that is in the user's path from a script, instead of hardcoding where Ruby is, you can use the following interpreter directive at the top of your script:

#!/usr/bin/env ruby

This works because pretty much all modern systems have an executable at /usr/bin/env which will execute the utility that you pass to it based on your path; so instead of hardcoding /usr/bin/ruby into your script, you can let env search your path for you.

Ruby interpreter name

Here is a Linux-only solution:

p File.open("/proc/self/cmdline") { |f| f.read.sub(/\0.*/m, "") }

For Ruby 1.8, ruby.c defines VALUE rb_argv0; which contains this information, but that variable is not available in Ruby scripts.

How to fix Unknown ruby interpreter version (do not know how to handle): RUBY_VERSION.

Coincidentally today I am also trying to setup Jekyll and am seeing the same problem. I am using RVM and it otherwise works fine (running multiple Rails dev sites locally). When I run env | grep 'RUBY' I get:

$ env | grep 'RUBY'
MY_RUBY_HOME=/Users/myusername/.rvm/rubies/ruby-2.0.0-p247
RUBY_VERSION=ruby-2.0.0-p247

However, I just continued and ran bundle install, then bundle exec jekyll serve and the site booted up without issue.

How can I get current depth of the Ruby interpreter stack?

Probably like this using caller:

caller.length

Keep in mind that starts at a certain depth when your program spins up, so you may want to subtract that from your count.



Related Topics



Leave a reply



Submit