Simple clear advice in plain English

Hands on: The many facets of Ruby

An insight into what the dynamic language has to offer, including blocks and closures

Ruby is a dynamic language that is exciting a lot of interest thanks to its excellent productivity.

In this feature, we will take a close look at the language to give those already familiar with alternatives such as Visual Basic or Java an idea of what Ruby offers, including blocks and closures.

Setting up Ruby

To recap, you can easily set up Ruby on Windows using the One-Click Ruby installer. If you want to create Windows applications, download the free ActiveTcl.

This lets you use the Tk cross-platform GUI toolkit. There are several options for a Ruby IDE – we used Eclipse 3.1 from with the Ruby Development Tools (both free) at www.rubypeople.org.

You can easily test your Ruby setup. From the Eclipse Window menu, choose Open Perspective and select Ruby. Next, go to File – New – Project and start a new Ruby project. With the project open, add a new file and call it main.rb. Type:

puts “It works”

Open the Eclipse Run dialogue. Create a new Run configuration, specifying main.rb for the file and ruby.exe for the Interpreter (under the Environment tab).

If Ruby is not yet listed, click the Add button to add the ruby.exe executable. In the Arguments tab, type ‘-w’. This switches on warnings. Then click Run.

Your Ruby program runs, and the output displays in the console. The ‘puts’ method writes to standard output. “Hello world” cannot get much shorter.

Five-minute Ruby

The starting point for most apps is a set of classes. This example will manage a magazine library, so start by declaring a couple of classes:

class Article
attr_accessor :subject
attr_accessor :rating
end
class Magazine
attr_accessor :title
attr_accessor :coverdate
attr_accessor :articles
end

You can put either these at the top of main.rb, or in a new file called, say, magazine.rb. If you do the latter, you can use the classes from main.rb provided you specify:

require ‘magazine’

before you start trying to use the classes declared there. Now write:

pcw = Magazine.new

pcw.title = “Personal Computer World”

#test the class – note the use of # for comments

puts pcw.title

You get the idea: doing objects in Ruby takes very little code. Note the use of the new method to get a new instance of a class.

Properties and methods

In the above example, a single statement, attr_accessor, creates a property. This is really a shortcut. The full version looks like this:

def title
@title
end
def title=(titleval)
@title = titleval
end

In Ruby, methods live in def… end blocks. The return value of a method is either specified with the return statement, or it is the value of the last statement executed.

The first method above returns the value of @title, while the second sets the value of @title. In other words, these are property getters and setters, just as in Visual Basic or C#.

If you do not need any further code, you can use attr_accessor instead.

The @ prefix has a special meaning in Ruby, indicating an instance variable (see Ruby conventions box).

Ruby classes support a method called initialize. Here is how to add a constructor to Magazine:

def initialize(title,coverdate)
@articles = Array.new
@title = title
@coverdate = coverdate
end

Now you can write:

pcw = Magazine.new(“PCW”,”June 2006”)

Can you have two constructors, one with and one without arguments? Not directly, because Ruby does not support method overloading.

This is one thing you might miss from other languages, but there are workarounds.

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Hands on: Programming with Ruby

Use it with Rails and find out how to create your web applications

Question & Answer

Q.Why can't my browser find the website address I typed...

> Read the answer

Q.All updates have been downloaded, so why won't Windows...

> Read the answer

Q.How do I stop Windows 7 search?

> Read the answer

Best deals on the web

img

Apple MacBook Pro (MC724LL/A)

£999.99- Buy it now

img

Sony Vaio VPCF23P1E/B

£679.98- Buy it now

img

Samsung 300E5A-A01DX

£449.99- Buy it now

Great benefits for subscribers!

Poll

Which is your preferred web browser

Jargon Buster

Computing terms explained in plain English

Restore point

A Windows backup of system files and settings.

Great shopping deals from Computeractive