Litt's Linux Blog, 12/2005

[ Older Blogs | Troubleshooters.com]

12/7/2005 @ 20:20: Node.rb

If you've read my writings or used any of my free software, you know I use a tool called Node.pm to handle hierarchical information and also to store data in memory. Node.pm has made the logic much simpler in many of my programs including EMDL and UMENU.

Yesterday and today I wrote Node.rb, a Ruby version of Node.pm. It appears to work very well, although a few of the more esoteric features aren't yet in it.

I found out that Ruby is available free (speech and beer) for 32 bit Windows, so I anticipate doing most new development in Ruby. The one negative factor is a lot of machines don't have Ruby installed. I'll need to find out how difficult Ruby is to install, and whether it produces dependency hell.

12/5/2005 @ 10:24: I REALLY like Ruby

Last Thursday night David Billsbrough gave an outstanding Ruby/MySQL presentation, inspiring me to create a Ruby tutorial, and a (so far very incomplete) Ruby subsite. In writing the tutorial I've fallen in love with Ruby for reasons hard to articulate. I just like it. Is it infatuation, or is it love? Is it just a summer thing?

If I had to put my feelings to words, I might say that Ruby spectacularly implements what Eric Raymond calls the Rule of Least Surprise. Every structure, every piece of syntax, is just what you'd imagine. After 4 hours of using Ruby, I found myself correctly guessing syntaxes I'd never seen before. You usually guess right, and when you don't, a simple error stack trace quickly leads you to the problem.

For years people have unsuccessfully urged me to learn Ruby. The problem was they all touted Ruby as a "fully Object Oriented" language, which I took to mean AOATT (All OOP All The Time). You know, like Java, where you must create a class to print "Hello World\n", and all subroutines must belong to a class, and therefore not be global.

80% of my programs are less than 500 lines, 30% are less than 100. Tiny programs like that do not justify an OOP design. There's no such thing as "quick and dirty" in AOATT.

Ruby is NOT AOATT! You can write a 5000 line Ruby program and never create a class (personally I would not do that, but you could). When people tell you Ruby is "fully Object Oriented", they mean that integers, strings, arrays, and all the things that in C would be built into the language, are objects in Ruby. In other words:

C
int a, b, c;
a = b + c;
In C, a, b and c are integers -- a type built right into the language. The plus sign is a plus operator, built right into the language.
Ruby
a = b + c
In Ruby, a, b and c are objects of the Fixnum class. The plus sign is a method of the Fixnum class.



C
strcpy(s, "Hello World");
int myLength = strlen(s);
In C, the syntax is generally functionname(operand).
Ruby
s = "Hello World"
myLength = s.length
In Ruby, the syntax is generally operand.functionname().

The difference is hardly earthshaking from the application programmer's point of view. Underlying this slight syntax difference is the fact that all operands are objects, and therefore can have methods built right into their classes. This eliminates the need to have, for instance, functions print_graphic(), print_calendar(), and print_musicscore(). Instead, classes graphic, calendar, and musicscore can each have their own print() method that knows exactly how to print that type of object.

Once again, from the application programmer point of view, the fact that all Ruby variables are objects simply requires slight syntax changes, but it's not an earthshaking paradigm shift.

When using Ruby, you must use objects, but you do not need to create objects.

That being said, OOP done right generally makes programs more scalable. Creating classes (and therefore objects) in Ruby is incredibly easy, and I've found that Ruby serves as a good practice tool for sharpening my OOP skills.

I highly recommend Ruby.

12/1/2005 @ 04:23: Ruby

Happy days are here again -- I'm writing regular material on GNU/Linux. The disasterous 2004 hurricane season caused the demise of Linux Productivity Magazine, but now I'm back with this blog. In conjunction with other Troubleshooters.Com GNU/Linux content, this blog will serve up new and contemporaneous GNU/Linux info.

By the way, writing GNU/Linux every time gets old, so I'll use the word Linux as an abbreviation, with the understanding that I'm talking about the Linux kernel and the GNU and other utilities.

Now let's talk about Ruby. David Billsbrough will present on "Ruby and MySQL" at tomorrow night's GoLUG meeting. In preparation I've begun studying Ruby, and what a surprise.

Chatter I've heard is that Ruby is one of those "all OOP all the time" languages. You know, like Java, where you need to make a class just to print "Hello World". I'm happy to report the chatter is wrong. Ruby authors procedural code with the best of them. My 4 hours' perusal indicates it's a very sane language warranting further investigation.

I'd like to write some more, but it's 4:45 in the morning, and even geeks need some sleep. More later.