From: Dave Thomas Date: 2001-05-08T21:44:28+09:00 Subject: [ruby-talk:14862] Re: Polymorphic methods "Benjamin J. Tilly" writes: > >> m = Month.new(12) > >> m = Month.new('December') > >> m = Month.new(anotherMonth) > > > >I'd like to know how to do that in ruby too, but my impression is > >that you just have to test the type. > But there is no way to remove the fact that if the type is to matter > then it must be tested, and logic that exists must be written. Ben: I think that might be a little absolute :) Wasn't it you who posted the double-dispatch example a couple of days ago? We could code the above without a condition in sight (apart from validation). class Integer def to_month if self >= 1 and self <= 12 self else raise "bad month" end end end class String def to_month { 'January' => 1, 'February' => 2, ...}[self] || raise "bad month" end end class Month def initialize(from) @month_number = from.to_month end def to_month @month_number end end Dave