Ruby For Programmers – Part 0: Using Ruby

Once you have Ruby installed you should see something similar to this when you type ruby -v in Terminal:

If you don’t, then you didn’t follow the previous instructions correctly.

What you have installed is the standard Ruby interpreter. Unlike a compiled language, such as C where the code is compiled into machine code, the interpreter takes Ruby code and interprets it as it goes. This is useful as you can redefine elements in code, which we’ll explain later.

Generally speaking, when referring to “Ruby” it generally means the Ruby Language using the MRI (Matz Ruby Interpretor, also known as “C Ruby” or standard Ruby). The MRI version is denoted as 1.x.ypz. The first two numbers denote the major version (1.x), the the minor version (y), and then the patch level (z). In the above version, you are running Ruby 1.9.3, patch version 286. There are big differences between major versions, such as the upgrade 1.8 to 1.9. When searching Google for answers, be aware that some really old gems made for 1.8 may not work. (Ruby as a community tends to be updateists, so anything relevant and used will support new versions quickly.) The minor version may have some small language changes, but usually upgrades. For example, Ruby 1.9.2 -> 1.9.3 brought much faster Date/Time methods by using native code instead of Ruby code to process them. While not necessarily breaking, you may find a weird catch or two when upgrading. Patch levels fix security bugs, while not breaking any part of the language.

Other than the 1.8 to 1.9 split, and again, 1.8 is dead, there’s no language splits.

While most people focus on MRI as their interpreter, there are other interpreters out there. JRuby is a Ruby interpreter made in Java. Depending on the benchmark, it is roughly similar to MRI in performance, and has better multi-threading support. However, it relies on Java so it has all the downsides to that. There’s also MacRuby, which converts its basics to Cocoa classes (NSString, NSArray, etc.), to be better able to make Mac/iOS apps. And Rubinus, a project defined as “Ruby in Ruby”, where they’re trying to reduce the amount of C code required.

For this tutorial, we will use MRI for all our examples. As such, we’ll show the most common tools of MRI.

Now that Ruby’s installed, let’s use it and create our hello world file:

When saved as helloworld.rb, simply use this command to run:

See, now wasn’t that easy?

Now let’s say you want to try out Ruby in a more interactive fashion, then you use irb:

As you can see, it will automatically print whatever you type. And if you tell it to print, it will print the output of the print function. We’ll get in to the details of the last statement later.

There’s one more helper function of ruby that you need to know, which is gem. Any language would be nothing without having a proper reusable code library, and rubygems provides a one-stop-shop for all your needs. As of Ruby 1.9, rubygems is pre-installed. So all you need to do to install a JSON handler, JSON being a popular data format, is the following:

And the gem ‘json’ is available to you. You can use this in irb to include:

Again, the details of the language we’ll explain later, just know that because you installed the json gem, the json library is available to use through the ‘require’ statement, and allowed you to convert that ruby data into JSON.

One thought on “Ruby For Programmers – Part 0: Using Ruby

  1. Pingback: Ruby for Programmers – Part 9: Making Pages With Bundler and Rack |

Comments are closed.