From: Harry Ohlsen Date: 2001-11-29T07:53:34+09:00 Subject: [ruby-talk:26846] Re: overload possible? Here's a different approach. I think it makes the usage a bit more "obvious", but that's only IMHO. The only issue I have with it is that this version doesn't catch the case where the particular set of argument types does not have a block of code associated with it. I think that's a significant problem, so if someone can see an elegant way to make it fall over if a method is called with an unexpected set of argument types, I'd love to hear it. Note that I wrote this just to see if it was possible and have never actually found a need to use it :-). This is in overload.rb ... module Overload def signature(args, *types, &block) argsSignature = (args.collect { |a| a.type.to_s }).join("-") typesSignature = types.join("-") if argsSignature == typesSignature block.call(*args) end end end So, to define a class Fred that has an overloaded method hello(), we could then do ... require "overload" class Fred include Overload def hello(*args) signature(args, :Fixnum, :String, :Float) do |i, s, f| puts "(Fixnum, String, Float) -> [ #{i}, \"#{s}\", #{f} ]" end signature(args, :String, :Fixnum) do |s, i| puts "(String, Fixnum) -> [ \"#{s}\", #{i} ]" end end end f = Fred.new f.hello(123, "hello", 123.456) f.hello("hello", 123)