Permalink
Browse files

add Fixnum#digits, about 2x faster than Integer#digits

  • Loading branch information...
1 parent 6447971 commit f5850f44e96c4c002175f2a17931f23c3ec91752 @JoshCheek JoshCheek committed Jun 28, 2011
@@ -1,3 +1,4 @@
+require 'project_euler/core_extensions/fixnum'
require 'project_euler/core_extensions/integer'
require 'project_euler/core_extensions/array'
require 'project_euler/core_extensions/enumerable'
@@ -0,0 +1,10 @@
+class Fixnum
+ def digits
+ working_int, digits = self, Array.new
+ until working_int.zero?
+ digits.unshift working_int % 10
+ working_int /= 10
+ end
+ digits
+ end
+end
@@ -0,0 +1,13 @@
+require 'spec_helper'
+require 'project_euler/core_extensions/fixnum'
+
+describe Fixnum do
+ describe '#digits' do
+ sample = [1,2,3,4,5,6,7,8,9,0] * 3
+ sample.each_index do |size|
+ crnt = sample.take(size.next)
+ next unless crnt.join.to_i.is_a? Fixnum
+ specify { crnt.join.to_i.digits.should == crnt }
+ end
+ end
+end

0 comments on commit f5850f4

Please sign in to comment.