Wednesday, November 21, 2007

Random Ruby Notes

I have been using Ruby much more than Perl these days to do all my dirty work in scripting most text handling tasks, which I find it is much better at doing these days. My only problem is mainly just the learning curve, which is typical of learning any software language, irregardless of how much conceptual and ideological similarities both languages may share.

Each language comes with it's own 'best practices', little idiosyncrasies and conventions that are unique, and so I'll try to present the following points that I've picked up for Ruby. Pardon me when I litter my subject headings with the 'right way' of doing things, it just means it's the conventional practice usually adopted by Ruby programmers.

The '?' or '!' behind the names of methods

They are both just a language convention. For the question mark, it is normally only for calls that return 'true' or 'false'. For the exclamation mark, it is used to mark that the method modifies 'self', rather modifying a copy while leaving the original unchanged.

It's helpful to note that having the punctuations behind the method names has no other significance besides that, something which is helpful to starters, especially if one is actually looking for an operational difference where there is none.

How do I know what class an object belongs to?

Either by:
object.class == Class

Or:
object.kind_of? Class

This applies only to Ruby being a full object oriented language. Perl does have OOP glued onto it, but seems quite ill-conceived that I hardly use it compared to the usual quick and dirty un-OOP that I'm used to.

The difference between the '==' operator and 'kind_of?' is that the former only matches the exact class, where kind_of? (probably) matches ancestor and descendant classes. I guess that's similar to the 'instanceof' keyword that I'm rather familiar in Java.

The right way for checking if a variable is defined
if defined? variable
puts "right way of checking variable is defined"
end

Where I've originally have been checking for 'defined-ness' by comparing to nil:
if variable == nil
puts "wrong way of checking variable is defined"
end

The right way for checking for array emptiness or non-emptyness

Rather than using arr.size == 0 or arr.size != 0, the two methods in ruby is arr.empty? or arr.any?. A comparison that checks the presence of an array is not a valid comparison for emptiness, i.e:
if [] then
puts "not the right way to check emptiness"
end

The above code is incorrect, because it will always return true as it checks for an array being defined, which is always true (for an empty array), rather than checking for the length of the array being zero.

Class loading mechanism in Ruby

If you needed to load a class definition dynamically, usually by passing the string of the class at Runtime, in Java the facility is:
Class c = System.getSystemClassLoader().loadClass("String");

The equivalent in doing this for Ruby:
s = Object.const_get("String");

0 comments:

Post a Comment