Starting with Ruby 16 Feb, 2011
What is Ruby? How do I get started? What’s this “Ruby On Rails”? All good questions, which I shall answer in good time. Put simply, Ruby is a fully object-oriented, interpreted programming language. It was developed some fifteen years ago by Yukihiro Matsumoto, and the underlying philosophy was to develop something that was enjoyable to use, and that abided by the “principle of least astonishment” (POLA). Unlike say, Java, Ruby is dynamically typed (and famously makes use of “duck typing” which I will cover in a future post). All you need know for now is that it’s pretty bloody nifty, and a welcome change from the verbosity of Java!
Naturally I have now convinced you to try it, so let’s start at the very beginning and set up the ole’ development environment. I’m working in OS X, but the command line stuff will be the same for everyone (you should be comfortable with firing up a terminal window and issuing the odd command, editing text files etc.) Windows users must not fret: there is a popular installer for your platform which does everything required to provide a functional command line interface for Ruby (oh, and don’t panic about all this mention of “command lines”: we won’t be doing anything esoteric).
On to installation
At the time of writing, v1.9.2-p136 is the latest and greatest version. If you already have Ruby installed (e.g. Leopard / Snow Leopard users), open a terminal session on your machine and issue the
ruby -v command thus (I also show a typical response—ignore the Darwin bit if you’re not on OS X):
$ ruby -v > ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0]
This will tell you what version you’re running. For OS X users, it won’t be the latest and greatest (1.8.7 comes with Snow Leopard I think), but no matter. For Debian and Ubuntu users, it is likely that you will need to install Ruby, and good old apt-get will do the do for you. Alternatively, you can pull the source from Git (or Subversion), and do a full build—this is how I updated Ruby to the latest version on my machine.
For Windows, your best bet is Luis Laverna’s RubyInstaller.org, which seems to be the de facto standard. Once you’ve done all the usual Windows installer stuff, you’ll end up with a Ruby program group in your Start menu. The option you’ll be tinkering with the most is “Start Command Prompt with Ruby”, as this fires up cmd.exe pre-configured for use with Ruby. Huzzah!
You can find more information on downloading and installing in the official downloads page, which includes more detailed instructions for Linux systems, plus Solaris and building Ruby from source.
For Mac users, Dan Benjamin maintains a great set of pages about updating Ruby, so check them out (I used his Snow Leopard page). Dan uses curl to download the source files, and the page is a little old now, so here’s the updated command for grabbing 1.9.2-p136 (note also that you can omit the download and build steps for RubyGems):
curl -O ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-1.9.2-p136.tar.gz
Once your Ruby installation is up to date, RubyGems will take care of any other stuff you have (e.g. Rails, database connectivity such as MySQL, and so on).
Hang on, “Rails”? “RubyGems”? What?
Yes I jumped ahead a little. Don’t sweat it. Think of RubyGems as an apt for Ruby (i.e. a tool for easily updating and installing Ruby dependencies and libraries), whilst Rails is a web development framework built on top of Ruby. We’ll come to this stuff in due course, trust me.
OK, now you have an install it’s time, obviously, to write a Hello World application (this is mandatory). Ruby source code is contained in plain text files, with the rb file suffix. Much like Java, you can run a Ruby program by passing the Ruby file to the ruby command like so:
$ ruby hello_world.rb > Why, hello there saucy
That’s skipping ahead though. Whilst we’re in the early days, and learning the language, it’s better to use the interactive shell environment Ruby provides, called irb (short for Interactive RuBy). It really is a shell: key in your commands, hit return, and off you go. So, here’s the command for whacking something out to the console:
$ irb irb(main):001:0> puts "Why, hello there saucy" Why, hello there saucy => nil irb(main):002:0>
You get the idea (BTW, to exit an irb session, type “exit” or use the end-of-file control-key sequence for your OS (CTRL-D on the Mac). Try some more things:
$ irb irb(main):001:0> 3+2091 => 2094 irb(main):003:0> Time.new => 2011-02-16 11:37:52 +0000 irb(main):004:0> irb(main):006:0> 99/3 => 33 irb(main):007:0>
Simple eh? Now, one final thing: before you start tinkering with irb on your machine, or if you haven’t yet installed Ruby and want to know a little more before diving in, run over to tryruby.org. This is a phenomenal tool which covers the basics of Ruby in a short (around 15 - 20 minutes) tutorial. It’s excellent, and well worth a play.
That’s probably enough for part 1. Do join me for part 2 won’t you? We’ll be writing some code an’ stuff!