Created
March 15, 2011 11:24
-
-
Save funny-falcon/870609 to your computer and use it in GitHub Desktop.
Monkey patch for speedup date/time parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Date | |
module Format | |
class Bag | |
A_FIELDS = %w{year mon mday hour min sec sec_fraction offset zone wday} | |
for fld in A_FIELDS + %w{_comp} | |
attr_accessor fld.to_sym | |
end | |
to_hash = A_FIELDS.map{|fld| "res[:#{fld}] = @#{fld} unless @#{fld}.nil?"}.join("\n") | |
class_eval <<-"END" | |
def to_hash | |
res = @elem.empty? ? {} : @elem.reject{|k,v| /\A_/ =~ k.to_s || v.nil? } | |
#{to_hash} | |
res | |
end | |
END | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems that it is not need for ruby 1.9.3 (future versions). But it will help with <=1.9.2.