In the first part of this guide we went through some foundational concepts in Rails development, and if you actually managed to read all the way through you’ll have noticed a series of download links at the very end. We’re gonna break the first one open now, the One-Click Ruby Installer, and get started down the road to Rails.
The Ruby Installer package for Windows has a whole bunch of interesting stuff inside it. Apart from Ruby and RubyGems, it also contains the SCiTE text editor and an excellent book called “Programming Ruby.” While writing this tutorial I used 1.8.5-22 Release Candidate 3, although you should get similar results with any version of this installer passed 1.8.5.
As this is a standard Windows installer, all you really have to do is double-click the setup utility. (No “configure, make, make install” here, fortunately.)
You’ll want to make sure “Ruby” and “Enable RubyGems” are checked (obviously). SCiTE is a free text editor that you may or may not want to try out. It’s small enough that it won’t impact your installation process significantly if you just keep this option checked. On the next step, you’ll be asked to specify the folder in which to install Ruby. I recommend going with the default “c:\ruby” here.
The whole installation process may take several minutes to unravel itself, due to the ten thousand or so pages of documentation that is installed on your hard drive. While that’s going on, let’s take a few minutes to familiarize ourselves with some key programming concepts. (Note that this is by no means an authoritative guide to programming in Ruby. For that, I recommend actually reading the book the comes with the Ruby installer.)
==================
Variables
A variable is an atomic part of any programming language. You can think of variables as little transparent plastic bags that you can place stuff into. Sometimes, it’ll be something really simple like a birthdate, or the name of a user. Other times, it’ll be something slightly more complex, like the URL to an image, or the first paragraph of a blog entry. Functionally speaking, there’s no limit to how big or how small that transparent plastic bag can be, and like any plastic bag, you can replace whatever’s already inside it with something else, any time you want.
In Ruby, we define variables like this:
@plastic_bag = “1981-04-12″
Here we have just placed my birthdate into a variable called “plastic_bag.” Generally speaking, we can call variables anything, but the more descriptive the name is, the better. The only rule you have to remember when defining variables is that the names cannot start with a number, so this is valid:
@birthdate = “1981-04-12″
but this isn’t:
@1birthdate = “1981-04-12″
Simple, huh?
Arrays
If variables are like plastic bags, arrays are just bigger bags that contain lots of individual plastic bags inside them. Arrays basically came about because it sucked to have to do something like this every time you were handling a collection of data:
@birthdate_1 = “1981-04-12″
@birthdate_2 = “1982-03-03″
@birthdate_3 = “1983-09-24″
What you’re looking at above are three variables, each defining a birthdate for some random person. If we re-wrote this information as an array, it would look like this:
@birthdates = ['1981-04-12', '1982-03-03', '1983-09-24' ]
Neater, and far more efficient. If you wanted to access one of these birthdates specifically, all you’d have to do would be to call @birthdates[0], which would return the first birthdate in the array. (Arrays always start with the number 0, instead of the number 1.)
Hashes
Now although arrays are great for simple, sequential lists, it’s often helpful to specify exactly what each item is for. In the example above, we have a list of birthdates, but what if we’d like to define whose birthdays they are? Hashes allow us to do this:
@birthdates = { ‘luis‘ => ‘1981-04-12′, ‘charlie‘ => ‘1982-03-03′, ‘lizz‘ => ‘1983-09-24′ }
Note the difference in punctuation: We use curly-braces ({) to denote a hash, and a square bracket ([) to denote arrays. In the hash example above, all we need to do to retrieve the birthdate of luis would be to call @birthdates['luis'].
=================
Ok so far? Our Ruby installation should be done by now, so let’s head over to our command line to try some of these commands out.
Windows has a fairly terminal application, which will cause you no end in consternation once you’ve really gotten into Rails development, but it’ll have to do for now. Start it up now (you can type cmd into the Run… field in the Start Menu to get there faster) and type “ruby -v” into the command-line.
You should get a response like this:
ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-mswin32]
If you did, then congratulations! You’ve got Ruby working on your machine. If you instead received an error message like this, then you will have to do one more thing to get it working:
‘ruby’ is not recognized as an internal or external command, operable program or batch file.
If you successfully completed the installation process, then this message just means that Windows doesn’t know where the Ruby interpreter actually is. These steps should solve that problem:
- Right-click on the My Computer icon, and select Properties from the context menu.
- Click on the Advanced Tab, then click on Environment Variables.
- In the bottom list box, there should be a variable called “Path” which should have a value that begins with “c:\ruby\bin;” (or wherever you installed Ruby). If it does not, you will need to add it manually.
- Restart your machine, run the terminal again and try typing “ruby -v”
- If it still doesn’t work, I would strongly consider re-doing the installation process, making sure that there are no other versions of Ruby installed on your machine.
At this point, it’s useful to talk about exactly what that command we’re typing actually does. Obviously the “ruby” keyword is what tells Windows that you’re calling the Ruby interpreter. The “-v” part is what we call a switch. Switches are like mini-commands; in this case, the “-v” switch tells Ruby to display its current version. There are a bunch of available switches, and you can get a list of them by typing “ruby -h” which display something like this:
There’s not a whole lot you can do with the ruby executable itself just yet, but there is one more thing that we need to be familiar with, before we go on to the next step. Ruby comes with a very interesting little application called irb (or interactive Ruby), which is useful for testing short Ruby scripts in real-time. To start it up, just type ‘irb‘ into your terminal.
Your cursor should change into something like irb(main):001:0> which is normal. That means you’re now inside the irb shell.
Now let’s try out some of the things we learned about variables, arrays and hashes above.
In the graphic above, I type in “@birthdates=['1981','1982','1983']” to define my array of birthdates. irb then returns the line “=> ["1981","1982","1983"]” to confirm that the values have been received. I then type @birthdates[0], and irb helpfully responds with the first birthdate on my list. On line 4, I redefine @birthdates as a hash, inserting the names of the celebrants of each date. Typing @birthdates['luis'] returns ‘1981,’ as expected.
To leave irb, just type exit. You’ll be returned to your normal terminal cursor automatically.
Now that you’ve confirmed that Ruby works, let’s move on to RubyGems. Type “gem -v” into the commandline to see what version of RubyGems you’re currently using (should be 0.9.2, at the time of this writing). You’ll also want to type “gem list” to see a list of the gems already installed on your system. The most important gem on that list is the one called “sources” which defines the locations of the various gems online. Without it, you will not be able to install Rails or any of the other stuff in the proceeding articles.
Next up: Rails, here we come!
Quick links to other articles in this series:
PART ONE: Introduction to Ruby, Rails, MySQL and Subversion.
PART TWO: Ruby Installation and Language Primer.
PART THREE: Rails, MySQL and the Windows Command Prompt.
PART FOUR: RMagick and Subversion.