Hi --

On Mon, 20 Jan 2003, 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 :-)

[...]

> i know that, in ruby, modules are usually used for things like this, but
> in my case many of the method names are the same so i don't think
> modules are approriate for this. for example, interface1 consists of
> foo, bar, baz, abc, and abd. interface2 consists of foo, bar, baz, bab,
> bac. in most of the cases all of the interfaces provide the same set of
> features, just in a different style or different revision.
>
> any pointers of how i might do something like this in ruby?

I wouldn't dismiss modules for this purpose.  For example:

  class C
    module Facet1
      def bar
	puts "bar version 1"
      end
    end

    module Facet2
      def bar(arg)
	puts "bar version 2, arg=#{arg}"
      end
    end

    def foo
      print "foo"
    end

    def facet(f=1)
      extend self.class.const_get("Facet" + f.to_s)
    end
  end

  C.new.facet(1).bar       # bar version 1
  C.new.facet(2).bar("hi") # bar version 1, arg=hi

Mind you, as Jim pointed out, once you have to know about facet(1) and
all that, you might as well bring it up front, which in my version
would mean doing:

  c = C.new.extend(C::Facet1)
  c.bar                        # bar version 1

or equivalent.  (extend is very cool :-)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav