From: Harry Ohlsen Date: 2001-11-29T08:13:36+09:00 Subject: [ruby-talk:26847] Re: overload possible? Here's a slightly better version, which also fixes the problem that the problem that the previous one didn't return the result of running the matching block. It IS a little messy, though, in that you have to sacrifice a class variable for the return value. Hmmm ... maybe Robert's version is looking simpler now :-). 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) true else false end end end Then you can write ... class Fred include Overload def hello(*args) if signature(args, :Fixnum, :String, :Float) do |i, s, f| puts "(Fixnum, String, Float) -> [ #{i}, \"#{s}\", #{f} ]" @result = "1" end elsif signature(args, :String, :Fixnum) do |i, s| puts "(String, Fixnum) -> [ \"#{s}\", #{i} ]" @result = "2" end else puts "Fred.hello() -- bad set of arguments" @result = "3" end @result end end f = Fred.new f1 = f.hello(123, "hello", 123.456) f2 = f.hello("hello", 123) f3 = f.hello("abc", "def") puts "f1 = #{f1} f2 = #{f2} f3 = #{f3}"