On Mon, 2003-01-20 at 08:00, David Garamond wrote:
> i want to have a class that can support multiple sets of methods, based 
> on what the client requests. so far i have written the following python 
> code. it's ugly/not elegant and i haven't managed to come up with a less 
> ugly ruby version :-)

Elegant is often in the eye of the beholder.  I have this ...

class C
  class << self
    def facet(n)
      facets = [C1, C2]
      facets[n-1].new
    end
  end
  def foo
    "foo"
  end
end

class C1 < C
  def bar
    "bar version 1"
  end
end

class C2 < C
  def bar(n)
    "bar version 2, arg = #{n}"
  end
end

class TestFacet < Test::Unit::TestCase
  def test_foo
    c = C.facet(1)
    assert_equal "foo", c.foo
    assert_equal "bar version 1", c.bar
  end

  def test_facet2_foo
    c = C.facet(2)
    assert_equal "foo", c.foo
    assert_equal "bar version 2, arg = 123", c.bar(123)
  end
end

But if the client knows enough to say "c = C.facet(2)", it seems to me
that it wouldn't be any harder to have the client say "c = C2.new". 
Then you can drop the the whole facet method in the Class, and the base
class no longer needs knowledge of its child classes (which is a good
thing).

-- 
-- Jim Weirich     jweirich / one.net    http://w3.one.net/~jweirich
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct, 
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)