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.
Related articles
Q.Why can't my browser find the website address I typed...
Q.All updates have been downloaded, so why won't Windows...
Q.How do I stop Windows 7 search?
We explain what internet radio is and how you can tune in to online stations
|
|
|
|
|
Nikon Coolpix S570 BlackPrice: £66.99 |
Computeractive Ultimate Guide - Storage, Sharing & BackupPrice: £5.99 |
Back Issue CD-Rom 13 (2010)Price: £9.99 |
Hallmark Card Studio DeluxePrice: £15.31 |
Marine AquariumPrice: £15.41 |