Where is the Ruby interpreter located?

SundayMonday picture SundayMonday · Nov 27, 2011 · Viewed 11.5k times · Source

I'm using Ruby 1.8.7 on OS X. Where is the Ruby interpreter located? My goal is to learn more about Ruby, interpreted languages and interpreting/parsing.

Answer

Brian Campbell picture Brian Campbell · Nov 27, 2011

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.