public
anonymous / gist:4440875
Created

change active_support String#to_date

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
require 'benchmark'
 
class String
def old_to_date
unless blank?
date_values = ::Date._parse(self, false).values_at(:year, :mon, :mday)
 
::Date.new(*date_values)
end
end
 
def new_to_date
::Date.parse(self, false) unless blank?
end
 
def blank?
false
end
end
 
date = '1/2/3'
 
Benchmark.bm do |x|
x.report('old') { 100000.times { date.old_to_date }}
x.report('new') { 100000.times { date.new_to_date }}
end
=> results
user system total real
old 0.950000 0.000000 0.950000 ( 0.952705)
new 0.940000 0.000000 0.940000 ( 0.945149)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.