william l kleb wrote:
> 
> It seems that this class is at least twice as long as it should be?  Also, as
> Dave and /\ndy suggest, it should also include a "cultural" unit test.

Here's our Ruby-newbie, unit-tested, pair-programmed, refactored version of Shape.rb:

class Shape
  attr_accessor :x, :y

  def initialize(initx, inity)
    @x, @y = initx, inity
  end
  def moveTo(newx, newy)
    @x, @y = newx, newy
    self
  end
  def rMoveTo(newx, newy)
    @x += newx
    @y += newy
    self
  end
  def ==(otherShape) # equality operator for (necessary?) for unit test assert
    @x==otherShape.x && @y==otherShape.y
  end
end

ShapeUT.rb:

require 'Shape'
require 'runit/testcase'
class ShapeUT < RUNIT::TestCase
  def setup
    @origin = Shape.new(0,0)
    @unity = Shape.new(1,1)
  end
  def test_moveTo
    assert_equal(@unity, @origin.moveTo(1,1))
  end
  def test_rMoveTo
    assert_equal(@origin, @unity.rMoveTo(-1,-1))
  end
end

require 'runit/cui/testrunner'
RUNIT::CUI::TestRunner.run(ShapeUT.suite)

Refactoring note: rMoveTo should probably be renamed to something like moveBy,
but we left it untouched to remain consistent with the guidelines given by
Weirich for the code comparison.

My question is the definition of the equality operator: is there some cleaner
way to have the assert pass without defining this?

Without something like this or explicitly asserting x and y values, the object
id's don't match, e.g.,

  ShapeUT.rb:9:in `test_moveTo'(ShapeUT):
    expected:<#<Shape:0x4584f80 @y=1, @x=1>>
     but was:<#<Shape:0x4584fc8 @y=1, @x=1>>
  (RUNIT::AssertionFailedError) from ShapeUT.rb:17

Thanks,
-- 
bil <http://abweb.larc.nasa.gov/~kleb/>