On 2003-08-08 07:27:08 +0900, Martin DeMello wrote:
[...]
> x.all? {|f| y.respond_to?(f)}

Some time ago I came up with a very similiar idea to check the interface
of an object in a case statement without using its class constant. The
advantage is that you can bind the created interface to a variable and
pass it around.

module Kernel

    def Interface(*selectors)
        interface = Object.new
        interface.instance_eval do
            @selectors = selectors
            def ===(obj) @selectors.all? { |s| obj.respond_to?(s) } end
        end
        interface
    end

end

has_split = Interface(:split)
[ %w[foo], "bar", "baz", 23 ].each do |thingy|
    print "#{thingy}: "
    case thingy
    when Interface(:join, :to_a)
        puts "join & to_a"
    when "baz"
        puts "is baz"
    when has_split
        puts "split"
    else
        puts "nothing"
    end
end

This prints:
foo: join & to_a
bar: split
baz: is baz
23: nothing

-- 
si tacuisses, philosophus mansisses.
  -- Boethius, Trost der Philosophie