From: Bertram Scharpf Date: 2007-10-22T17:19:00+09:00 Subject: Re: How does overload work in ruby? Hi, Am Montag, 22. Okt 2007, 14:25:49 +0900 schrieb Xavier Noria: > On Oct 22, 2007, at 7:17 AM, Shuaib Zahda wrote: >> is there any special way for overloading in ruby. >> >> class Overloading >> >> def sum(a, b) >> return a + b >> end >> >> def sum(a, b, c) >> return a + b + c >> end > > Ruby does not support method overloading that way. [...] > However, you can define defaults > for optional parameters like this: > > def sum(a, b, c=0) > a + b + c > end 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 Some methods even change their behaviour with the number of parameters they are called with, for example File#basename. You still may check whether the last parameter is nil. If you need to be 100% sure, you could write a C function; rb_scan_args() returns the number of arguments given. Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de