From: Harry Ohlsen Date: 2001-11-30T04:35:10+09:00 Subject: [ruby-talk:26951] Re: overload possible? In article <200111291631.fATGVQu20572@sharui.nakada.kanuma.tochigi.jp>, "nobu.nokada" wrote: > At Thu, 29 Nov 2001 14:05:00 +0900, > Rich Kilmer wrote: >> excellent idea...how about this refactoring... > > I thought up another trick. > class Fred > def hello(*args) > case args = Signature[*args] > when Signature[Fixnum, String, Float] > i, s, f = args > puts "(Fixnum, String, Float) -> [ #{i}, \"#{s}\", #{f} ]" "1" > when Signature[String, Fixnum] > s, i = args > puts "(String, Fixnum) -> [ \"#{s}\", #{i} ]" return "2" > else > raise args.invalid > end > end > end > > f = Fred.new > f1 = f.hello(123, "hello", 123.456) > f2 = f.hello("hello", 123) > f3 = (f.hello("abc", "def") rescue $!) > > puts "f1 = #{f1.inspect} f2 = #{f2.inspect} f3 = #{f3.inspect}" To me, this is a perfect syntax for it !! I might even pass that one by the person who pointed me at the perl multimethods stuff that he thought was realy nifty ... it's nowhere near as neat as this :-).