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.
You can start it by typing irb in your shell and hitting enter. Its name is short for “Interactive Ruby Shell”, and yes, it is another kind of shell: Just like the shell running in your terminal irb is also a program that interactively waits for you to type something, and hit enter.
The original Ruby interpreter is often referred to as Matz's Ruby Interpreter or MRI. This implementation is written in C and uses its own Ruby-specific virtual machine. The standardized and retired Ruby 1.8 implementation was written in C, as a single-pass interpreted language.
The Ruby interpreter first scans the file for BEGIN statements, and executes the code in their bodies. Then it goes back to line 1 and starts executing sequentially. See BEGIN and END for more on BEGIN .) Another difference between Ruby and compiled languages has to do with module, class, and method definitions.
Ruby is a compiled language in much the same way that Java is. While ruby is not compiled down to native machine code, it is compiled into a set of bytecode instructions that are interpreted by a virtual machine.
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.
whereis ruby
in a Terminal window will tell you
You should find it under
System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
and symlinked to
/usr/bin/ruby
.
running which ruby
will give you the exact location of the ruby being used if there are one or more implementations on your system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With