From: William James Date: 2009-02-11T16:48:59+09:00 Subject: Re: how to do the recursion Li Chen wrote: > William James wrote: > > >> If you found Pascal's code somewhat puzzling, let me explain. > > His customary language is CLisp, so when he deigns to dabble > > in Ruby, he tries to make his code look lispish. > > > Hi Will, > > Thank you for your rubyish code. > > Yes. I am really confused when I read Pascal's codes. I don't think > nowhere in Pixaxe ever mentions about function BUT only methods and > objects! > > I have questions about two code lines: > 1) result << > > What does it mean, concatenate nothing??? That line is not complete by itself. The following lines provide either 1 or 0 to be concatenated. > > 2) num.odd? > When I run the code Ruby complains about #odd?. I check the class > Fixnum and I can't find #odd? So I change it to num%2==1 and Ruby > runs happily. It works with ruby 1.8.7 (2008-05-31 patchlevel 0). You may have an older version. > > > Thanks, > > Li > > > > > > 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 --