Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit f5850f44e96c4c002175f2a17931f23c3ec91752 1 parent 6447971
@JoshCheek authored
View
1  project_euler/lib/project_euler.rb
@@ -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'
View
10 project_euler/lib/project_euler/core_extensions/fixnum.rb
@@ -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
View
13 project_euler/spec/lib/core_extensions/fixnum_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.