Learn to Program

Getting Started

Chapter 0


When you program a computer, you have to "speak" in a language your computer understands: a programming language. There are lots and lots of different languages out there, and many of them are excellent. In this tutorial I chose to use my favorite programming language, Ruby.

Aside from being my favorite, Ruby is also the easiest programming language I have seen (and I've seen quite a few). In fact, that's the real reason I'm writing this tutorial: I didn't decide to write a tutorial, and then choose Ruby because it's my favorite; instead, I found Ruby to be so easy that I decided there really ought to be a good beginner's tutorial which uses it. It's Ruby's simplicity which prompted this tutorial, not the fact that it's my favorite. (Writing a similar tutorial using another language, like C++ or Java, would have required hundreds and hundreds of pages.) But don't think that Ruby is a beginner's language just because it is easy! It is a powerful, professional-strength programming language if ever there was one.

When you write something in a human language, what is written is called text. When you write something in a computer language, what is written is called code. I have included lots of examples of Ruby code throughout this tutorial, most of them complete programs you can run on your own computer. To make the code easier to read, I have colored parts of the code different colors. (For example, numbers are always green.) Anything you are supposed to type in will be in a dotted box, and anything a program prints out will be in a grey box.

If you come across something you don't understand, or you have a question which wasn't answered, write it down and keep reading! It's quite possible that the answer will come in a later chapter. However, if your question was not answered by the last chapter, I will tell you where you can go to ask it. There are lots of wonderful people out there more than willing to help; you just need to know where they are.

But first we need to download and install Ruby onto your computer.

Windows Installation

The Windows installation of Ruby is a breeze. First, you need to download Ruby. There might be a couple of versions to choose from; this tutorial is using version 3.0.0, so make sure what you download is at least as recent as that. (I would just get the latest version available.) Then simply run the installation program. It will ask you where you want to install Ruby. Unless you have a good reason for it, I would just install it in the default location.

In order to program, you need to be able to write programs and to run programs. To do this, you will need a text editor and a command line. My favorite text editor is Sublime Text.

It would also be a good idea to create a folder somewhere to keep all of your programs. Make sure that when you save a program, you save it into this folder.

To get to your command line, select Command Prompt from the Accessories folder in your start menu. You will want to navigate to the folder where you are keeping your programs. Typing cd .. will take you up one folder, and cd foldername would put you inside the folder named foldername. To see all of the folders in your current folder, type dir /ad.

And that's it! You're all set to learn to program.

Macintosh Installation

If you have Mac OS X 10.2 (Jaguar) or later, then you already have Ruby on your system! What could be easier?

In order to program, you need to be able to write programs and to run programs. To do this, you will need a text editor and a command line.

Your command line is accessible through the Terminal application (found in Applications/Utilities).

For a text editor, you can use whatever one you are familiar or comfortable with. My favorite text editor is Sublime Text. If you use TextEdit, however, make sure you save your programs as text-only! Otherwise your programs will not work.

And that's it! You're all set to learn to program.

Linux Installation

First, you will want to check and see if you have Ruby installed already. Type which ruby. If it says something like /usr/bin/which: no ruby in (...), then you need to download Ruby, otherwise see what version of Ruby you are running with ruby -v. If it is older than the latest stable build on the above download page, you might want to upgrade.

If you are the root user, then you probably don't need any instructions for installing Ruby. If you aren't, you might want to ask your system administrator to install it for you. (That way everyone on that system could use Ruby.)

Otherwise, you can just install it so that only you can use it. Move the file you downloaded to a temporary directory, like $HOME/tmp. If the name of the file is ruby-1.6.7.tar.gz, you can open it with tar zxvf ruby-1.6.7.tar.gz. Change directory to the directory you just created (in this example, cd ruby-1.6.7).

Configure your installation by typing ./configure --prefix=$HOME). Next type make, which will build your Ruby interpreter. This might take a few minutes. After that is done, type make install to install it.

Next, you'll want to add $HOME/bin to your command search path by editing your $HOME/.bashrc file. (You might have to log out and back in again for this to take effect.) After you do that, test your installation: ruby -v. If that tells you what version of Ruby you have, you can now delete the files in $HOME/tmp (or wherever you put them).

And that's it! You're all set to learn to program.