August 14, 2007
More on Ruby
If the previous post made it look like I think everything is just perfect with Ruby -- well, it isn't perfect.
For instance, Let's take a look at a basic class like the Array class, which in an effort to be Humane violates several OO principles.
For one it violates Don't Repeat Yourself (DRY) by including several methods that do the same (#length and #size, for instance). More importantly it violates the Single Responsibility Principle by exposing
Stack (#push and #pop methods) and Queue (e.g. #shift). Making an Array double as a queue is not just violating SRP, it is also implementation revealing rather than intention revealing.
Another even more annoying problem is the sometime confusing scoping :
If you do something like:
class Foo
attr_accessor :bar # create accessor methods for a bar attribute
def barTender(value)
bar=value
# do something with bar
end
end
What happens here is that the bar in bar_tender is a private variable of the bar_tender class so setting it doesn't affect the bar attrribute. If you want to set the bar property, you need to call that with self.bar
.
Fortunately, the IDE I am using (Netbeans) has recently added a warning for this issue so I won't be doing these again.
I also find it annoying that a dynamic language like Ruby is also case sensitive so if you call something like barTender and bartender you'd get two different methods/variables etc. I guess that's why the Ruby convention is to use all lowercase, i.e. bar_tender. ( Maybe I'll switch to that as well. You know, when in Rome..)
Oh yeah and why the hell does elsif miss the "e" ? :)
And there are, of course, other things. I do have to say though, that on average I find Ruby code to be more readable than Java or C# . I guess it is easier to define small code DSLs in Ruby (or other dynamic
languages) than it is in Java or C# ,but I'll write about that in a different post.
Posted by Arnon Rotem-Gal-Oz at 05:00 PM Permalink
|