Learning Ruby 1 – Hello World!

It’s entirely possible that your area of science doesn’t require you to process data. Maybe you have a wonderful job where all you do is synthesis, and all your characterisation is done by hapless Honours students, and the only things you need to note are that this one has an 80% yield and gave you a wonderful orange colour.

But chances are, at some point you’ll use a characterisation method that produced three files per sample, and suddenly you’ll end up with fifteen million graphs in .csv format; or you have a bunch of images that you really need to compare, or maybe you just need to run the same calculation over and over again, only changing a few variables.

Research is about collecting and applying knowledge, and at its heart, almost all knowledge can be presented in text form or shoved into a table. But in a niche field you’ll never find the software you really want to do what you have to do by hand. It’s situations like these that you need some coding ability.

Enter ruby

There’s plenty of different programming languages out there, and each has its fanatics. They all have their benefits and their drawbacks. In this series I’m going to talk about ruby, an interpreted, high-level, functional programming language that I have been using over the past three or four years to put together various projects.

In this introductory article, all I’m going to do is tell you a bit about ruby itself, what it’s good for, and what it sucks at. Then, I’ll give a little demo of how to install it and run your first program. Over the course of the following articles I’ll slowly introduce the language to you – starting at the basics, like assignment and methods, moving onto classes and modules, taking a quick tour around rubygems and some pretty awesome libraries, and perhaps at the end getting into the coolness that is meta-programming.

Introduction to the language

Ruby was developed in the mid-1990s by Yukihiro Matsumoto (known as “Matz” to most of the ruby community), and rose to prominence around 2000 with the development of Ruby on Rails, an open-source web framework that was designed specifically for the kind of dyanmic webapps that were just starting to come out around then. Of course, it has far more uses than just programming for the web – it’s a pretty flexible language that can lend its hand to all kinds of problems.

Ruby is high-level

One of the awesome things about ruby is that it’s one of these so-called “high level” programming languages that have come to prominence recently. In the old days, when computer disk space was measured in bytes, you needed to know exactly how each bit of memory was being used. You didn’t want to leave 2kB lying wasted because your program was keeping track of a variable it no longer had a use for. This led to the development of low-level programming languages like C, in which every bit is accounted for, and you have incredibly granular control over what got kept and what got thrown away.

These days, of course, you have RAM measured in GB and disk space measured in TB, so those extra few kilobytes where you’re holding something you don’t actually need, don’t really matter. You’d prefer to spend time focussed on the problem at hand, rather than where you’re going to store a string. For this reason, languages like ruby run automated garbage collection, which basically runs behind you as you code and picks up all the stuff you aren’t usine any more. This means that you spend less time fiddling, and more time getting what you want to get done, done.

Ruby is interpreted

There are two forms of programming language – compiled and interpreted. Compiled languages are written and then run through a compiler to make a machine-code program. Compiled “binaries”, as they’re known, are written for a particular platform and don’t need a translator to sit between them and the computer to tell it what to do. They’re incredibly fast, but also suffer from some drawbacks: namely, that you can’t run one binary on another OS (e.g. run a Windows-compiled binary on Mac OS X), and you can’t go from binary back to code.

Ruby is an interpreted language. That means no compile step – your program is saved as plain-text, and that text is then run through an interpreter, which tells the computer what to do as it reads the code. This is sometimes called “just-in-time” compilation, as the interpreter only compiles the code into machine language as it needs to. Interpreted languages tend to be much slower than compiled languages, but this does mean that your code will run anywhere and anyone can see how you did something.

Ruby is dynamic

One of the other wonderful things about being an interpeted language is that you can start to edit yourself on-the-fly. This is a bit more advanced than previous stuff, but it can come in useful for some applications. Any explanation I attempt here is probably going to be either confusing or in-depth, so let’s just remember that ruby can be dynamic and we’ll come back to it later.

Ruby is functional

Similar to the comment above about ruby being dynamic, ruby’s functional roots are something that will come in handy later on rather than here in our first delve into the language. Basically, functional programs can end up treating blocks of code as “bits of paper” to be passed around. I can pass a bunch of code from one end of the program to the other, and have it get run at the end of the program without anything in between knowing what it does except for the big picture view. As with before, if I go any further into this I’ll start using words like “introspection”, and the goal of this tutorial is to help people, not alienate them with jargon.

What do I need to know?

In this tutorial I will be assuming that you know little or nothing about coding. However, I do need to assume that you know how to use the command line. At the very least, have a quick google and read up a bit, and work out how to list files (dir on Windows, ls on Linux and OS X) and change directories (cd). You may also want to look into making folders, working out where you are, and deleting files by the command line.

Installing ruby

Right! If you’re still reading this you’re probably somewhat interested about giving programming in ruby a go. Well done! I can’t guarantee we’ll have you coding masterpieces in a week, but we’ll see if at the end of this tutorial series you can’t do something fancy with code.

Some operating systems come with ruby pre-installed. To see if this is the case, open up your terminal or command prompt and type in ruby -v. When I do that on my machine I get: ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10.2.0], which is just ruby telling me what version it’s running. If you get something similar, congratulations! You have ruby installed on your machine. You may want to visit the ruby website and grab a newer copy – although I should warn you that installing it is a bit more advanced than this article covers. If you’re comfortable with the terminal, go right ahead.

The ruby webpage

If you get a command not found or similar message, it looks like you’ll have to install ruby.

Installing ruby isn’t too bad – in this article I’ll assume you’re running Windows. If you’re running Mac OS X, you’ll have some form of ruby installed anyway. If you’re running a Linux variant and you don’t have ruby installed, try apt-get or the variant that comes with your flavour of linux.

Ruby for Windows comes as an installer, which you can grab from rubyinstaller.org. Download it, open it, and let it install ruby to your hard drive. Now when you run ruby -v from your command prompt, you should get a nice version message like I was getting.

Hello, world!

OK, now we’re ready to start programming. The traditional place to start programming is with a program that prints “Hello, world!” and quits. With ruby, that’s pretty simple.

First, open up notepad or your text editor of choice. If you’re going to be doing a bit of this, I suggest finding a text editor that supports syntax highlighting – notepad++ is supposed to be quite good, plus it’s free.

In your new document, simply type the following:

puts “Hello, world!”

Now save this file as “helloworld.rb”. Note that ruby files traditionally use the .rb file extension. Finally, open your command prompt, cd into the directory you saved your ruby file into, and type ruby helloworld.rb.

(Note how I was saying about ruby being an interpreted language before? ruby is the interpreter – it reads the file helloworld.rb and tells the machine what to do as it goes through the file.)

If you did everything right, you should get something like mine:

Hello, world!

Conclusion

That’s all for this first article. I’ve told you what ruby can do, and you’ve now got it installed and working properly. In my next article, we’ll start checking out what we can do inside of ruby, and how we can start messing with files.

Leave a Reply

Your email address will not be published. Required fields are marked *