Skip to content

Instantly share code, notes, and snippets.

@jaydorsey
Created May 12, 2025 16:08
Show Gist options
  • Save jaydorsey/f0f5c14697487e362fb5c48235fa635d to your computer and use it in GitHub Desktop.
Save jaydorsey/f0f5c14697487e362fb5c48235fa635d to your computer and use it in GitHub Desktop.
Date parsing alternative
# A lot of times, dates will be handled like this:
#
# parsed_date = Date.parse(maybe_date) rescue nil
#
# This is an alternative, faster strategy that doesn't rely on a catch-all, less performant
# (in error conditions at at least; backtraces aren't generated), rescue behavior
def parse_date_string(maybe_date)
return if maybe_date.blank?
catch(:date) do
parsed_date = Date._parse(maybe_date).slice(:year, :mon, :mday)
throw :date, Date.new(*parsed_date.values) if parsed_date.size == 3
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment