Hitchhiker's Guide to Software Architecture and Everything Else - by Michael Stal

Sunday, January 02, 2005

Ruby

It is a long time since my last post. I will try to write more regularly this year. Today, I'd like to talk about a programming language which I am really excited about. As I prepared an introductory language tutorial in the last weeks for the OOP 2005 conference I had to intensify my learning and knowledge. The language is Ruby. It was developed 1993 by a japanese guy and originally intended to be a better Perl than Perl. Nonetheless it denotes a pure OO language (whatever that means :-). There are a lot of benefits using Ruby: the language itself, the libraries, the community. What I like most is its combination of different things, OO concepts from languages such as Java, features from Smalltalk and Lisp, and elements taken from scripting languages.

Let me just show you an example:

class RubyExample
def testMethod
if block_given?
yield("Here is Michael")
end
end
end

v = RubyExample.new
v.testMethod { \arg\ print arg }

The class above just includes one method. You might pass a block to the method which will then be called within the method's body. In the block the argument passed by the method will be printed to the console. Th example itself is somewhat boring. However, the feature it presents is very powerful. Blocks might be used to implement iterators, threads, or closures, to name just a few examples.

Another fascinating aspect of Ruby is its usage of Mix-Ins instead of multiple inheritance.
Suppose, you implemented the following module:

module M
def info
"42" # btw, the last statement in a method also is the return value
end
end

In your class you might include the module

class C
include M
end

If you now instantiate C you'll be able to use the included method:

c = C.new
c.info ==> "42"

Methods will be shared by the class including the module. All instance variables defined within the module will have separate counterparts in the including class. Basically, a class behaves like a singleton class.

Ruby offers some interesting built-in types such as ararys, maps, ranges, regular expressions, symbols. Especially regular espressions are essential for the scripting folks.

The libraries available for Ruby allow you to implement GUIs (using Tk), access Win32, build distributed applications, access databases, implement unit tests, process XML, build Web sites, and uncountable other sorts of applications.

Of course, Ruby is free to use although there are also commercial products out there.

If you are now curious about Ruby just go to my home page and download the tutorial slides.

0 Comments:

Post a Comment

<< Home