Skip to content

Instantly share code, notes, and snippets.

@funny-falcon
Created March 15, 2011 11:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funny-falcon/870609 to your computer and use it in GitHub Desktop.
Save funny-falcon/870609 to your computer and use it in GitHub Desktop.
Monkey patch for speedup date/time parsing
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
@funny-falcon
Copy link
Author

It seems that it is not need for ruby 1.9.3 (future versions). But it will help with <=1.9.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment