Done puzzle 42
[project-euler.git] / array-numbers.rb
1 # Some utility extensions for helping manipulate numbers
2 # and lists of digits
3
4 class Array
5 # Convert a list of digits into a number
6 def to_i
7 self.map {|d| d.to_s}.join.to_i
8 end
9
10 # Convert a two-item list of nubmers into a rational
11 def to_r
12 Rational(self[0].to_i, self[1].to_i)
13 end
14 end
15
16 class Integer
17 # Split a number into a list of digits
18 def to_digits
19 self.to_s.split('').map {|d| d.to_i}
20 end
21 end