From: pjb@... (Pascal J. Bourguignon) Date: 2009-02-11T18:48:55+09:00 Subject: Re: how to do the recursion Li Chen writes: > William James wrote: > >> def dec_to_binary num, result = [] >> result << >> if num.odd? >> 1 >> else >> 0 >> end >> if num < 2 >> result.reverse.join "" >> else >> dec_to_binary num/2, result >> end >> end >> >> >> p dec_to_binary 254 > > > Hi Will, > > I have one more question about the method call: > > When you define method dec_to_binary, you pass two parameters. > But when you call the method, you only provide one parameter. > > Surprisingly Ruby doesn't complain it. How to explain this seemly > inconsistancy? That's possible because we specified a default value of some of these parameters. William specified an empty array as the default for result: result = [] I specified an empty string: result="" That renders the corresponding argument optional. (Of course, all the parameters with defaults must be last in the paramater list, and the optional arguments are assigned in order). -- __Pascal Bourguignon__