I like Enumerator goodness. I wrote this code for a project of mine:
class Integer
def digits
Enumerator.new do |x|
to_s.chars.map{|c| x << c.to_i }
end
end
end
This gives you access to all the good Enumerator stuff:
num = 1234567890
# use each to iterate over the digits
num.digits.each do |digit|
p digit
end
# make them into an array
p num.digits.to_a # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
# or take only some digits
p num.digits.take(5) # => [1, 2, 3, 4, 5]
# you can also use next and rewind
digits = num.digits
p digits.next # => 1
p digits.next # => 2
p digits.next # => 3
digits.rewind
p digits.next # => 1