Ruby - Power, Simplicity, and Beauty

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.

7 Responses to “Ruby - Power, Simplicity, and Beauty”

  1. Morgan Schweers Says:

    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

  2. Morgan Schweers Says:

    ...oops, that really didn't work out. Preview would have been nice... You get the idea, hopefully.

  3. Brian Buckley Says:

    and then to...

    def gettotalcharge rentals.sum &:get_charge end

  4. Cody Fauser Says:

    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.

  5. marcia Says:

    i love your blog

  6. Rodrigo Kochenburger Says:

    And it can even get more sexy!

    rentals.sum(&:get_charge)

    :)

  7. Margo Says:

    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

Sorry, comments are closed for this article.