First Time Linux

Ruby

Ruby is a relatively young language (since only the mid-90s) with many similarities to python and perl. However it has many characteristics which are all its own. Like those other two, more famous languages, ruby is suitable for programming simple scripts with very short development time, and also larger programs. It has quite a consistent approach which makes it easy to pick up, and can also use Qt libraries to build GUI programs, as we'll see. Its popularity has produced many frameworks and communities around the language, most notably the web framework "Ruby on Rails".

Getting and running

Getting ruby is of course very straightforward, just look for "ruby" in the package manager. Or from the command line just urpmi ruby is enough to get everything you need. The current package given with Mandriva 2008.1 is ruby-1.8.6-9p114.1mdv2008.1. After install, we can check the version number:

$ ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [i586-linux-gnu]

(In Debian wheezy you need aptitude install ruby and this gives the version ruby 1.9.3p194 (2012-04-20 revision 35410) [i486-linux].)

Confusingly if you don't give the "-v" and just type the "ruby" command then it looks like nothing is happening - this is because, like perl, the interpreter is now waiting for you to give in your commands. One simple thing to try is:

5.times { print "hello" }

and then execute this 'program' by entering Ctrl-D. Note that those are curly brackets marking a block to execute 5 times, not round brackets.

This "times" construction is unusual, and interesting because it's actually a method called on the object 2. Everything is an object in Ruby, there are no primitive types.

But there's a better way to run ruby programs interactively, and that's called the "interactive ruby console" or irb. This runs each command as you enter it (like python's does) rather than just at the end. So it's much better suited to experimenting and trying things out. You can run this from the console like this:

$ irb
irb(main):001:0;> print "hello from ruby"
hello from ruby=> nil
irb(main):002:0> my_variable = 2
=> 2
irb(main):003:0> print my_variable, " ", my_variable * 2
2 4=> nil
irb(main):004:0>>>> my_variable = "now a string"
>>> print my_variable, my_variable * 2
now a string now a stringnow a string

This is all very similar to python, in the way that variables are declared and dynamically redeclared. Except for the funny prompts (including an incrementing command number) and the funny "nil"s (which are the result of the print command, ie this command doesn't give a result back).

Also note that there's another way to run a ruby console, and that's to run it in your browser! So you don't even have to install ruby on your computer to try it out! Amazing. Just go to tryruby.org and make sure your javascript is enabled.

Running a saved script

Now let's save our commands in a file, so we can run it any time we like. Use an editor (doesn't matter whether it's a graphical one like Kate or a console one like vi, use whatever you prefer) and create a new file called hello.rb:

my_array = ['carrot', 'artichoke', 'aubergine', 'antelope']
puts "The array is:"
my_array.each { |thingy| puts thingy }
puts "so the sorted list is " + my_array.sort.join(", ")

After saving this file, we can give it straight to the interpreter:

$ ruby hello.rb
The array is:
carrot
artichoke
aubergine
antelope
so the sorted list is antelope, artichoke, aubergine, carrot

This simple example shows the definition of a simple string array, the looping (with the "each" keyword), the sorting and concatenation of list elements. As you can see, semicolons aren't necessary to break up the commands and the code is fairly readable even at first glance.

Making the file executable

Just like with bash scripts, perl and python, we can make our hello.rb executable so we don't have to type ruby <file> to run it, but just <file>. As before, edit the file to include the following line as the first line of the file:

#! /usr/bin/env ruby

Next we need to give ourselves execute permission for this file:

$ chmod u+x hello.rb

and then we can execute it directly:

$ ./hello.rb

Making a GUI

As with python, for Ruby to use the Qt libraries you need the bindings which for Mandriva are called "ruby-qt" - installing this pulls in another couple of packages for the generic Qt bindings and then you're set.

Now to test the library with a simple gui:

require 'Qt'

app = Qt::Application.new(ARGV)

button = Qt::PushButton.new("Hello from Ruby!", nil)
button.resize(100, 30)
button.show()

app.exec()

Now save this file as rubygui.rb and run it as before using ruby rubygui.rb. Magically we get a GUI window with a single, functionless button inside it, just as before, and again the window resizing, minimizing and closing is all handled for us already.

screenshot of Qt
Screenshot of our Qt example using Ruby

Obviously this gui looks exactly the same as the python and C++ examples, because it's using exactly the same Qt code underneath. That's the magic of the Qt bindings. And because ruby and Qt are cross-platform, you've got cross-platform gui applications with very little effort.

Note of course there are plenty of other gui libraries you can use with ruby, Qt is just one example.

More info

The official home page is ruby-lang.org which has a lot of useful information including documentation. There's also Wikipedia's Ruby page for some basic background. For an impressively bizarre tutorial, the like of which you have never seen before, try to find the Poignant Guide to Ruby (original now taken down). And for the interactive ruby console in your browser without installing anything, see tryruby.org.

For the web framework built around Ruby, see Wikipedia's Ruby on Rails article.

Update: the inspirational author of the poignant guide (he calls himself "why the lucky stiff") has disappeared and mysteriously taken his content down. So if you want the poignant guide (and you probably do), see the archived version instead.