PreviousNext…

Starting with Ruby

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: puts:

$ 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!

Comments

  1. Fun stuff! Getting really into it these days. People should check out railscasts too. Wait till Domino people find out how easy web dev is in rails 3. Gem/Bundle is amazing.Jim Knight#
  2. In the same "spirit", but in Java, you should check Play!Framework : very web oriented, extremelly productive…without all java "heavyness"…Michael Bourak#
  3. @2 Yes I’ve tinkered with Play, and it’s pretty nifty. Interestingly, it works on the same premise as Ruby on Rails, in that it’s “opinionated software”, favouring convention over configuration.

    A link for those interested in knowing more:
    http://www.playframework.orgBen Poole#
  4. The default suggested name is Ben Poole in the Your name (required) field. Cool.
    The Play Framework looks very nice. It seems to have many similarities with Grails. I'll will have a look.
    A ruby tutorial is cool too although as an Admin it is not easy to distinguish between all the cool languages.
    I recently find myself trying to add semicolons to Lotusscript code.Henning Heinz#
  5. I haven't played with Ruby for a few years, but still read a few of the RoR blogs - from what I've seen the cool kids are also using RVM to manage Ruby versions these days: http://rvm.beginrescueend.com
    I've been playing with Django/Python lately and I think it's similiar to virtualenv on that platform - where you can have an environment consisting of a version of Ruby and associated Gems for a particular site, that could be different for a different site on your local machine and you can easily switch between the environments when developing. Sounds more complex then it is when I try and explain it like that :-)Marcin#
  6. Yes, RVM is the recommendation in the “Pickaxe” (“Programming Ruby - The Pragmatic Programmers Guide”) and I may cover that later on!Ben Poole#
  7. Another catchy blog, which has prompted a question - How can you easily migrate Ruby (RoR) code and applications to other languages, such as php, java and c #? Are there any tools out there that help?Jon#
  8. Ruby, PHP and Java are all pretty different languages, so any migration effort is going to be largely manual / involved. But if you’re a Java developer intrigued by the RoR approach, in addition to the aforementioned Play Framework, Grails is the thing to look at: http://grails.orgBen Poole#
  9. hehe.. yeah.. I feel your pain So Rails is getting bitetn by the eat-your-dog-food thing.. Rails framework code uses Ruby (I suppose?) and whatever you write in Ruby for your Rails app can clash with the framework code itself? Am I making sense? Maybe this is my ignorance talking. I haven't started working in this arena that much.Anyways, nice to know you are enjoying the new environment. I do keep checking your blog from time to timeNil#

Comments on this post are now closed.

About

I’m a developer / general IT wrangler, specialising in (often mobile-friendly) web apps using things like node.js, Java, C#, PHP, HTML5, etc.

Best described as a simpleton, but kindly. Read more…