From: Robert Dober Date: 2007-01-28T08:58:38+09:00 Subject: Re: Question about digits ------=_Part_60913_11875383.1169942316701 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 1/28/07, CHubas wrote: > > While playing a little with Ruby, I've been looking for a function > each_digit, or something similar, and I couldn't find any (standard > nor library). I think it'd be useful to have a function like that. > It's pretty simple to implement one for Integers > > class Integer > def each_digit(base = 10, &block) > return if zero? > (self/base).each_digit(base, &block) > yield self % base > end > end > > A first approach. Of course, it would be a little more complicated for > negatives and Floats, specially dealing with precision. > > What do you think? class Integer def each_digit(base=10) to_s( base ).each_byte{ |b| yield b.chr } end #def each_digit(base=10) end Cheers Robert -- "The best way to predict the future is to invent it." - Alan Kay ------=_Part_60913_11875383.1169942316701--