Created
May 12, 2025 16:08
-
-
Save jaydorsey/f0f5c14697487e362fb5c48235fa635d to your computer and use it in GitHub Desktop.
Date parsing alternative
This file contains hidden or 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
# 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