Issue #14672 has been updated by shevegen (Robert A. Heiler).


I can understand the proposal.

I think your primary use case is to be able to use less code,
without needing to rescue all (or the important) errors specifically
via begin/rescue.

Perhaps it can be mentioned in the ruby developer meeting to see how
matz feels about API and usage pattern. (Also in general, e. g. for
all future proposals that aim to reduce explicit begin/rescue
clauses; see also how 'pp' became usable by default, without being
required to use an explicit "require 'pp'" line anymore since a
while).

As method name I would think Date.failsafe_parse() may be good too :D

Alternatively, perhaps use a shorter name, but provide an additional
argument possibility, such as :exception or something like that, to
allow for the above behaviour.

----------------------------------------
Feature #14672: Introdice a Date.safe_parse method
https://bugs.ruby-lang.org/issues/14672#change-71438

* Author: coorasse (Alessandro Rodi)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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:

```ruby
Date.safe_parse(nil) 
=> # nil

Date.safe_parse("") 
=> # nil

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



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>