Chris wrote:
> <Sigh> Yep.  Which is one of the reasons Ruby is appealing to me.  It's
> A LOT OF STINKING WORK to create even the simplest Perl OO module, I
> have to admit.  Versus:
>
> class Foo
>     attr_reader :bar, :boat
>
>     def initialize( bar, boat )
>        @bar, @boat = bar, boat
>     end
>
>     def concat
>        @bar + @boat
>     end
> end
>
> foo = Foo.new
> p foo.concat
> p foo.bar
> p foo.boat
>
> Wow.  And you have accessors and everything right there.  In Perl, the
> above is easily 10-20 lines of code and the less lines, the more
> ambigious it would be.  The above Ruby is just plain nice.


Damn right.  It's amazing how after years of programming the simple things
still take ages to properly sink in.  I needed a counter for several
indices a few days ago.  I only needed it in one place.  Instead of using
a hash and managing all the logic inline (where lots of other logic was
taking place), a simple class worked wonders.

    #
    # Maintains a bunch of indexed counters.
    #
    #   counter = Counter.new
    #   counter.next('x')          # -> 1
    #   counter.next('y')          # -> 1
    #   counter.next('x')          # -> 2
    #   counter.next('z')          # -> 1
    #   counter.next('y')          # -> 2
    #   counter.next('x')          # -> 3
    #
  class Counter
    def initialize
      @counters = {}
    end

    def next(idx)
      @counters[idx] ||= 0
      @counters[idx] += 1
    end
  end

My favourite refactoring pattern at the moment is to move all the related
private methods of a class into their own class.  It helps to clarify the
real responsibility of the original class, to highlight the relationship
between them, and to test the supporting code more easily.  (The difficult
part is thinking of a name for the new class.)

To do all this, you need a language where creating new classes is easy and
intuitive.  Having a language that's ball-tearingly powerful doesn't hurt,
either.

Cheers,
Gavin