From: tenderlove@... Date: 2016-11-28T23:01:39+00:00 Subject: [ruby-core:78405] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception Issue #12732 has been updated by Aaron Patterson. Hi, > Is there any problem with the following code? > Integer(str) rescue default_value 2 problems 1. It's slower than it could be (as Naruse demonstrates) 2. It's very noisy when `-d` is enabled. In Psych, I am trying to avoid noise from `-d`. That means I have to try to check if the string will work with `Integer()`, then actually call `Integer()`. It means the string has to be parsed twice. If `Integer(str) rescue default_value` didn't make noise, then I would be OK with that. :) ---------------------------------------- Feature #12732: An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception https://bugs.ruby-lang.org/issues/12732#change-61778 * Author: Aaron Patterson * Status: Feedback * Priority: Normal * Assignee: Yukihiro Matsumoto ---------------------------------------- I would like to be able to pass an option to `Integer()` and `Float()` so that they don't raise an exception, but return `nil` instead. For example: ~~~ Integer(string, exception: false) ~~~ The reason I want this function is so that I can convert strings from YAML or JSON to integers if they parse correctly, or just return strings if they can't be parsed. ---Files-------------------------------- integer-parse.pdf (29 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: