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.
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
···
On 1/28/07, CHubas <CHubas7@gmail.com> 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?
--
"The best way to predict the future is to invent it."
- Alan Kay
Good. I'd expect each_digit to return strings though, since a digit is a symbol, not a number:
class Integer
def each_digit(base=10)
abs.to_s(base).each_byte do |b|
yield b.chr
end
end
end
-- fxn
···
On Jan 28, 2007, at 12:35 AM, 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.
> 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.
Good. I'd expect each_digit to return strings though, since a digit
is a symbol, not a number:
class Integer
def each_digit(base=10, want_sign=false)
yield '-' if want_sign && self < 0 # to continue debugging
my code;)
abs.to_s(base).each_byte do |b|
···
On 1/28/07, Xavier Noria <fxn@hashref.com> wrote:
On Jan 28, 2007, at 12:35 AM, CHubas wrote:
yield b.chr
end
end
end
-- fxn
--
"The best way to predict the future is to invent it."
- Alan Kay