C
CHubas
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?
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?