From: Daniel Brockman Date: 2005-08-26T02:49:09+09:00 Subject: Re: idea: klass.from_s(str) Eric, > With the String#to_* methods, you have to do something > like this: > > def parse_option(klass,option) > if klass.equal?(String) > option > elsif klass.equal?(Integer) > option.to_i > elsif klass.equal?(Float) > option.to_f > else > raise("can't convert #{option} to #{klass}") > end > end > > With klass.from_s(str) methods, here is what you get: > > def parse_option(klass,option) > klass.from_s(option) > end > > Which is duck-typed? As David said, duck typing is irrelevant in these examples. Duck typing is where you don't care about what class (Duck, WalkingQuackMachine) some given object is an instance of, but rather what messages (walk, quack) it responds to. How do you map that to the `parse_option' example? I'd describe the difference between your two snippets by saying that the former uses *external* polymorphism (which is widely considered bad OO design) while the latter uses *internal* polymorphism (essential to good OO design). Kind regards, -- Daniel Brockman