From: Arlen Christian Mart Cuss Date: 2007-10-23T22:39:13+09:00 Subject: Re: How does overload work in ruby? On Mon, 2007-10-22 at 21:31 +0900, Rick DeNatale wrote: > > If you want to allow the caller to select the default value > > explicitly you may say > > > > def sum a, b, c = nil > > c ||= 0 > > a + b + c > > end > > It seems to me that this is just a slightly less efficient way of > doing the same thing as the code you quoted. > > In either case the caller isn't specifying the default value, it's > overriding the default. > > In the call sum(1,2) ruby will evaluate the default expression for c, > in the call sum(1,2,3) it won't since c has a value given by the third > parameter. Making the default value of c nil, and then doing c ||= 0 > justs slows down both cases. It also means that if the caller really wants to pass nil, it'll get turned to the default value. - Arlen