From: dblack@... Date: 2007-01-13T01:12:13+09:00 Subject: Re: Why doesn't Ruby allow for overloaded methods within a class Hi -- On Fri, 12 Jan 2007, Wes Gamble wrote: > (from a Java refugee) > > All, > > I've done quite a bit of googling and read a bunch of posts about this, > but I'm looking for a simple explanation of why Ruby doesn't support > method overloading within a class to allow methods with the same name > but different numbers of arguments. I understand that you can't do > method overloading with the same # of arguments since you don't know any > "types" of input parameters when you invoke a method. > > But...it seems like you could allow it for different numbers of input > parameters. > > Here's an example of what I mean: > class Foo > def methname(x,y) > ... > end > > def methname(x, y, z) > ... > end > end > > I'm comfortable with how to implement this using a method with varargs > and appropriate logic, and I'm aware of arguments that the methods > should probably be different in name if their set of input parameters > are of different size. > > But it seems like this kind method overloading would be doable in Ruby. > Is it possible? > Is it not available because there's a feeling that it isn't needed? I've never extensively used any language that has this feature, but it's always struck me as a bit strange, and very brittle. What if you want two method behaviors, but both behaviors involve having the same number of arguments? It's always seemed to me a bit like having methods dispatch on what letter of the alphabet their parameters begin with. So... I guess my view is that Ruby has evolved beyond it, though there may be some use for it that I'm just not seeing. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)