Project

General

Profile

Actions

Feature #14672

open

Introduce a Date.safe_parse method

Added by coorasse (Alessandro Rodi) over 3 years ago. Updated over 3 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:86488]

Description

This feature request is about a Date.safe_parse method.
The method should call the original Date.parse method but avoid raising an exception and returning a fallback value instead.
An implementation in ActiveSupport has been proposed here: https://github.com/rails/rails/pull/32503/files.
This would avoid the necessity to rescue possible Exceptions everytime.

See:

Date.safe_parse(nil) 
=> # nil

Date.safe_parse("") 
=> # nil

fallback = Date.new(2018,1,1)
Date.safe_parse(nil, fallback)
=> # #<Date: 2018-01-01>
Actions

Also available in: Atom PDF