Tonight while reading through Refactoring by Martin Fowler I was once again amazed by power, beauty, and simplicity of Ruby. Wait, hold on, aren't the code examples in Refactoring in Java? Yes, that is precisely the point. All day I am lucky enough to write Ruby, but sometimes I need to take a look back over the other side of the fence to appreciate where I'm at. Let me tell you that the grass is definitely not green over there.
Consider the simple method being extracted on page 27 of the book:
1 2 3 4 5 6 7 8 9 10 |
private double getTotalCharge() {
double result = 0;
Enumeration rentals = _rentals.elements();
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getCharge();
}
return result;
}
|
Basically the code is just iterating over the collection of Rental objects and calculating the total charge. Simple enough. The equivalent using inject with Ruby:
1 2 3 4 |
def get_total_charge rentals.inject{ |sum, rental| sum + rental.get_charge } end |
Then, with the new sum method added to Enumerable in Rails it gets even simpler:
1 2 3 4 |
def get_total_charge rentals.sum{ |r| r.get_charge } end |
This just goes to show the power and expressiveness of Ruby. With so many fewer keystrokes, and lines of code, it is so much easier to focus on the problem at hand.
Sorry, comments are closed for this article.
March 15th, 2007 at 12:54 AM
Greetings, First off, I'm not disagreeing, I love Ruby and it's a real joy to program in. It is worth noting, however, that Refactoring was written pre-generics. I don't have it in front of me, but this should work:
private double getTotalCharge() { double result = 0; for(Rental rental : _rentals) result += rental.getCharge(); return result; }It's not that bad.I have my head in both worlds regularly (and a touch of C++ as well), so I try to keep my balance. Implicit return values in Ruby was something it took a bit to really internalize, and the example above relies heavily on them, for instance.
-- Morgan
March 15th, 2007 at 12:56 AM
...oops, that really didn't work out. Preview would have been nice... You get the idea, hopefully.
March 15th, 2007 at 06:35 PM
and then to...
def gettotalcharge rentals.sum &:get_charge end
March 16th, 2007 at 10:11 PM
Morgan,
That does look a bit better, but it still isn't quite as sexy ;)
Brian,
I was going to show that method as well, which is even more concise, but I just don't like how the method calls look with the &:, so I never use it.
March 19th, 2007 at 05:32 PM
i love your blog
March 30th, 2007 at 05:45 AM
And it can even get more sexy!
rentals.sum(&:get_charge)
:)
April 3rd, 2007 at 06:26 AM
huh... this world has nothing to do with rooshing and shrooshing, does it. i consider myself multi-disciplinary, and considerably capable of conversational talent of a variety of subjects, but i am lost--no geeky ruby humour lies in me... i'm happy to read that it is sexy tho!
you look awesome in the vie edit cody! i can't imagine watching that under any influence, let alone that of jamie's toys.... i hope you are able to see it with a clear and embracing eye now... you were awesome.. i loved most your gentle resistence and obvious limits to a lot of the commercial torture that we subject you to.... but the payoff is fun, right?? if only to meet someone like diane! o.k.. i know. this is the RUBY forum.. over and out. xx