What is Ruby and why is it useful? This article will touch on the history and features of the Ruby language, and some of the reasons you might want to have a deeper look at Ruby.

The world is filled with programming languages, but most of them are destined for obscurity. No one can be quite sure what propels a language from the wide, broad bench of "interesting" to the celebrity red carpet of "popular", but when you see it happen it's probably time to have a deeper look at the language and see if it's useful to you. Ruby is one of these breakthrough languages.

Ruby is frequently characterised as a "scripting language", but since many people seem to equate "scripting language" with "toy" this sells Ruby short. Ruby is a fully object-oriented, dynamic programming language. It was released by Yukihiro Matsumoto ("Matz") in 1995, and for many years it was relatively unknown outside of Japan, until two key events triggered an explosion of interest. In 2000 Andy Hunt and Dave Thomas (the Pragmatic Programmers) wrote Progrmmaing Ruby, the first English-language reference to Ruby, and in July, 2004 "Ruby on Rails", a Web application development framework by David Heinemeier Hansson, was released. Ruby on Rails used reflection and other dynamic attributes of Ruby to make Web application development fast and easy, and quickly became the "killer app" for Ruby. There are certainly other useful Web application development frameworks, but none of them has experienced the explosive growth we've seen from Rails.

So what are the characteristics of Ruby? First, everything in Ruby is an object, including things like integers and floats that are treated as primitive types in other languages. It's open, in the sense that every class can be extended, and most existing behaviour can be overridden. It's interpreted -- there is no compilation step. This means that it's very easy to make changes to your code and quickly see the effects, but it also means that Ruby applications generally run more slowly than their compiled language equivalents. There is currently a lot of work going into alternative Ruby runtimes -- new Ruby interpreters, and approaches to running Ruby in existing environments such as the Java VM and the .NET CLR.

Ruby is dynamically typed. In the Ruby community this is known as duck typing -- "when I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck". For a Ruby object to work in a context (say, as a method argument), it only needs to implement the methods that are actually invoked in that context -- not necessarily the "complete" set of methods that you might associate with some abstract type. This is very flexible, but means that typing errors can only be determined at runtime. Ruby developers commonly use extensive unit tests to catch these conditions, rather than relying on a compiler. Ruby has included a unit testing framework since 2001.

Ruby is text-based. All the source code is stored in text files, and can be searched and manipulated with all the Unix command line tools that we know and love (or their equivalents in other operating systems). Being text-oriented also means that Ruby source code integrates smoothly into our version control system of choice.

Ruby is terse, and also frequently provides multiple ways to achieve the same end. This lets the developer choose the most expressive syntax for their specific context -- it's a humane interface rather then a minimalist interface. The set of reserved words in Ruby is quite small -- many of the actions that are keywords in other languages are implemented in Ruby as methods on some object. This is one of the ways that Ruby provides flexibility, including alternative implementations for the same actions. This doesn't mean Ruby doesn't have its quirks -- it certainly does, and the variety of choices means it can take a while to learn all the Ruby idioms.

Let's dive into some Ruby code so we can see some examples of how this plays out in the real world. All you need to get started is a Ruby interpreter and access to the command line. On OS X Ruby is included by default; on Windows, there is a Ruby One Click installer (see the references); and on Linux you'll probably need to download and install a Ruby package. We can execute Ruby directly from the command line without even creating a file:

	> ruby -e "puts('Hello World!')"
	Hello World!

The -e flag tells Ruby to treat the next parameter as code to be executed. In this case we've used "puts", a method that prints its arguments to STDOUT with newlines. Parentheses are optional in Ruby method invocations, and you'll more commonly see this written as:

	> ruby -e "puts 'Hello World!'"

Ruby code is line delimited -- by default the end of a statement is the end of the line -- but you can also use a semicolon as a delimiter:

	> ruby -e "puts 'Hello World!';puts 'Hello Ruby!'"
	Hello World!
	Hello Ruby!

Of course, there's a limit to what we can achieve executing statements at the command line, so Ruby can also execute the contents of a file. The default extension for a Ruby file is .rb.

	# file: hello_world.rb

	puts 'Hello World!'
	puts 'Hello Ruby!'

	> ruby hello_world.rb
	Hello World!
	Hello Ruby!

This also illustrates another Ruby convention -- file names, method names and variable names usually use underscores to separate words, rather than the camel case convention of other languages, such as Java. However, Ruby does use camel case for class names, as we'll see shortly. Also, anything following a # character is a comment.

Since I've said that Ruby is purely object-oriented, some of you may be wondering what object is receiving the method "puts". In Ruby the current object can be referenced using the pseudo-variable "self".

	> ruby -e "puts self.class"
	Object

When we're executing statements at the command line they're being sent to an instance of Object created by the Ruby interpreter.

Now let's quickly look at the syntax for creating a class.

	# file: say_hello_01.rb

	class SayHello

	    def say_it
	        puts 'Hello World!'
	        puts 'Hello Ruby!'
	    end

	end

	puts SayHello.new.say_it

Here we've defined a new class, a instance method called "say_it", then created a new instance of the class and invoked "say_it" on that instance. This shows part of the flexibility of Ruby -- we can create classes and run scripts that depend on those classes in the same file. Although many Ruby files will contain class definitions that match the name of the file, a Ruby file is free to contain whatever it wants. Once again, this is very flexible but can require a bit of a mental shift for programmers accustomed to, say, Java, where looking for a public class is the same as looking for a file of that name.

Here's the same class, but this time taking some parameters:

	# file: say_hello_02.rb

	class SayHello

	    def say_it(message1, message2)
	        puts message1
	        puts message2
	    end
  
	end

	SayHello.new.say_it('Hello World!', 'Hello Ruby!')

Notice that the parameters don't have any type declarations, and the method doesn't have a return type; since Ruby is dynamically typed we don't need to specify the types (and in fact, we can't!)

Do you need help with Ruby? Gain advice from Builder AU forums

Related links

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

Log in


Sign up | Forgot your password?

  • Staff Apple to developer: Fart jokes aren't funny

    When Apple announced it would be vetting every application submitted for inclusion in the App Store, this was just the kind of question that entered many a mind: just how arbitrary would the company be in wielding that veto power? Read more »

    -- posted by Staff

  • Staff Chrome is just another browser

    Hands up if you missed the Chrome release -- didn't think anyone did. Google's browser arrived with all the fanfare and hype that only Google can produce. Read more »

    -- posted by Staff

  • Renai LeMay 2Vouch refers well

    Melbourne-based Web start-up 2Vouch yesterday launched the first public beta of what it dubs its "social recruiting platform". Read more »

    -- posted by Renai LeMay

What's on?