ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:77171] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
@ 2016-09-07  5:13 ` tenderlove
  2016-09-07  6:55 ` [ruby-core:77180] " tenderlove
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: tenderlove @ 2016-09-07  5:13 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been reported by Aaron Patterson.

----------------------------------------
Feature #12732: An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
https://bugs.ruby-lang.org/issues/12732

* Author: Aaron Patterson
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
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.



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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:77180] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
  2016-09-07  5:13 ` [ruby-core:77171] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception tenderlove
@ 2016-09-07  6:55 ` tenderlove
  2016-10-11 11:16 ` [ruby-core:77576] " shyouhei
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: tenderlove @ 2016-09-07  6:55 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by Aaron Patterson.

File integer-parse.pdf added

Adding a slide to show code I'm actually writing vs want to write

----------------------------------------
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-60394

* Author: Aaron Patterson
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:77576] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
  2016-09-07  5:13 ` [ruby-core:77171] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception tenderlove
  2016-09-07  6:55 ` [ruby-core:77180] " tenderlove
@ 2016-10-11 11:16 ` shyouhei
  2016-11-25  7:01 ` [ruby-core:78307] [CommonRuby Feature#12732][Feedback] " matz
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: shyouhei @ 2016-10-11 11:16 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by Shyouhei Urabe.


We looked at this issue in developer meeting today.

It seems originally, ruby was designed under assumption that string to integer conversion in general could be covered 100% by either to_s or Integer().  Truth is we need the proposed functionality.

People at the meeting was not sure about the API though.  Is it a variant of Integer() or a separate new method?  For instance an attendee suggested "Integer?()" but could not be popular.

----------------------------------------
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-60847

* Author: Aaron Patterson
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:78307] [CommonRuby Feature#12732][Feedback] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2016-10-11 11:16 ` [ruby-core:77576] " shyouhei
@ 2016-11-25  7:01 ` matz
  2016-11-25  9:34 ` [ruby-core:78347] [CommonRuby Feature#12732] " naruse
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: matz @ 2016-11-25  7:01 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by Yukihiro Matsumoto.

Status changed from Open to Feedback
Assignee set to Yukihiro Matsumoto

Is there any problem with the following code?

```
Integer(str) rescue default_value

```

Matz.

----------------------------------------
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-61670

* 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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:78347] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2016-11-25  7:01 ` [ruby-core:78307] [CommonRuby Feature#12732][Feedback] " matz
@ 2016-11-25  9:34 ` naruse
  2016-11-28 23:01 ` [ruby-core:78405] " tenderlove
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: naruse @ 2016-11-25  9:34 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by Yui NARUSE.


Below is PoC; it may have a path which raises an exception.

```diff
diff --git a/object.c b/object.c
index 05bef4d..5d63803 100644
--- a/object.c
+++ b/object.c
@@ -2750,17 +2750,60 @@ static VALUE
 rb_f_integer(int argc, VALUE *argv, VALUE obj)
 {
     VALUE arg = Qnil;
+    VALUE opts = Qnil;
+    VALUE exception = Qnil;
+    VALUE vbase = Qundef;
     int base = 0;
+    static ID int_kwds[1];
 
-    switch (argc) {
-      case 2:
-	base = NUM2INT(argv[1]);
-      case 1:
-	arg = argv[0];
-	break;
-      default:
-	/* should cause ArgumentError */
-	rb_scan_args(argc, argv, "11", NULL, NULL);
+    rb_scan_args(argc, argv, "11:", &arg, &vbase, &opts);
+    if (!NIL_P(vbase)) {
+	base = NUM2INT(vbase);
+    }
+    if (!NIL_P(opts)) {
+	if (!int_kwds[0]) {
+	    int_kwds[0] = rb_intern_const("exception");
+	}
+	if (rb_get_kwargs(opts, int_kwds, 0, 1, &exception)) {
+	    VALUE tmp;
+	    if (RB_FLOAT_TYPE_P(arg)) {
+		double f;
+		if (base != 0) goto arg_error;
+		f = RFLOAT_VALUE(arg);
+		if (FIXABLE(f)) return LONG2FIX((long)f);
+		return rb_dbl2big(f);
+	    }
+	    else if (RB_INTEGER_TYPE_P(arg)) {
+		if (base != 0) goto arg_error;
+		return arg;
+	    }
+	    else if (RB_TYPE_P(arg, T_STRING)) {
+		const char *s;
+		long len;
+		rb_must_asciicompat(arg);
+		RSTRING_GETMEM(arg, s, len);
+		tmp = rb_cstr_parse_inum(s, len, NULL, base);
+		if (NIL_P(tmp)) {
+		    return exception;
+		}
+		return tmp;
+	    }
+	    else if (NIL_P(arg)) {
+		if (base != 0) goto arg_error;
+		return exception;
+	    }
+	    if (base != 0) {
+		tmp = rb_check_string_type(arg);
+		if (!NIL_P(tmp)) return rb_str_to_inum(tmp, base, TRUE);
+arg_error:
+		rb_raise(rb_eArgError, "base specified for non string value");
+	    }
+	    tmp = convert_type(arg, "Integer", "to_int", FALSE);
+	    if (NIL_P(tmp)) {
+		return rb_to_integer(arg, "to_i");
+	    }
+	    return tmp;
+	}
     }
     return rb_convert_to_integer(arg, base);
 }
```
```ruby
def assert(a, b)
  if a != b
    raise "'#{a}' != '#{b}'"
  end
end
def assert_raise(ex)
  begin
    yield
    raise "#{ex} is expected but not raised"
  rescue ex
    # correct
  rescue
    raise "#{ex} is expected but #{$!.inspect}"
  end
end
o = Object.new
assert 123, Integer("123")
assert 50, Integer("32", 16)
assert 16, Integer("10", 16, exception: o)
assert o, Integer("x", exception: o)
assert o, Integer("x", 16, exception: o)
assert_raise(ArgumentError){ Integer("x") }
assert_raise(ArgumentError){ Integer("x", 16) }
```
```ruby
require'benchmark/ips'
Benchmark.ips{|x|
  x.report("rescue") {
    Integer('foo') rescue nil
  }
  x.report("kwarg") {
    Integer('foo', exception: nil)
  }
}
```
```
Warming up --------------------------------------
              rescue    36.258k i/100ms
               kwarg    64.004k i/100ms
Calculating -------------------------------------
              rescue    392.926k (± 8.9%) i/s -      1.958M in   5.025204s
               kwarg    844.563k (±14.9%) i/s -      4.096M in   5.017539s
```

----------------------------------------
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-61713

* 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/

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [ruby-core:78405] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2016-11-25  9:34 ` [ruby-core:78347] [CommonRuby Feature#12732] " naruse
@ 2016-11-28 23:01 ` tenderlove
  2017-02-07 11:56 ` [ruby-core:79467] " mail
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: tenderlove @ 2016-11-28 23:01 UTC (permalink / raw
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:79467] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2016-11-28 23:01 ` [ruby-core:78405] " tenderlove
@ 2017-02-07 11:56 ` mail
  2018-01-24  7:54 ` [ruby-core:85031] " matz
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: mail @ 2017-02-07 11:56 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by Jan Lelis.


Although it does not solve Aaron's use case, I would suggest to have a `Integer.try_convert`, `Float.try_convert`, `Rational.try_convert`, and `Complex.try_convert` which do not raise exceptions, but just return `nil`. To keep consistency, they would just call implicit, then explicit conversion (e.g. `to_int`, then `to_i`), instead of `Integer()`'s special parsing.

It only allows strict parsing, but is much cleaner, imho. Also, it would fill some empty spots in the [core conversion table](http://idiosyncratic-ruby.com/54-try-converting.html#core-classes-conversion-table) and make Ruby's conversion logic simpler. (`.try_convert`, which currently feels more like an implementation detail, could then be embraced more).

To allow `Integer()` special conversion, it would still need an `exception:` option, but also `Float()`, `Rational()`, and `Complex()` would need it (since they currently also lack this feature due to not having a `try_convert`). One idea is to give every of the uppercased Kernel methods an `exception:` option, but this does not make sense for `Array()` and just would not be needed if going for the broader `try_convert` support).

To summarize my suggestion in two bullet points:

- Create `Integer.try_convert`, `Float.try_convert`, `Rational.try_convert`, and `Complex.try_convert` which prefer implicit conversion (if available), then explicit conversion, but return `nil` instead of raising an exception
- Give `Integer()` an `exception` option to support special integer parsing without exceptions, but do not give `Float()` one

----------------------------------------
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-62897

* Author: Aaron Patterson
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Target version: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:85031] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2017-02-07 11:56 ` [ruby-core:79467] " mail
@ 2018-01-24  7:54 ` matz
  2018-01-24  8:00 ` [ruby-core:85032] " knu
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: matz @ 2018-01-24  7:54 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by matz (Yukihiro Matsumoto).


Aaron's comment in #note-6 sounds reasonable. Accepted.

Matz.


----------------------------------------
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-69742

* Author: tenderlovemaking (Aaron Patterson)
* Status: Feedback
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:85032] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2018-01-24  7:54 ` [ruby-core:85031] " matz
@ 2018-01-24  8:00 ` knu
  2018-02-07 19:17 ` [ruby-core:85465] " tenderlove
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: knu @ 2018-01-24  8:00 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by knu (Akinori MUSHA).


Just for the record, `Integer(x, rescue: default_value)` might be an idea, if anything other than `nil` (like zero) would be desired.

----------------------------------------
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-69743

* Author: tenderlovemaking (Aaron Patterson)
* Status: Feedback
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:85465] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2018-01-24  8:00 ` [ruby-core:85032] " knu
@ 2018-02-07 19:17 ` tenderlove
  2018-02-08 12:49 ` [ruby-core:85478] " nobu
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 13+ messages in thread
From: tenderlove @ 2018-02-07 19:17 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by tenderlovemaking (Aaron Patterson).


Something like `Integer(x, rescue: default_value)` is fine for me too, (or `Integer(x, ->() { default_value })`, which is similar to `[].find(->() { missing_value }) { ... }`)  Configuring with a default value seems more flexible.

----------------------------------------
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-70256

* Author: tenderlovemaking (Aaron Patterson)
* Status: Feedback
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:85478] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (9 preceding siblings ...)
  2018-02-07 19:17 ` [ruby-core:85465] " tenderlove
@ 2018-02-08 12:49 ` nobu
  2018-02-08 17:47 ` [ruby-core:85484] " tom.enebo
  2018-05-29 11:35 ` [ruby-core:87296] " oliveira.matheussilva
  12 siblings, 0 replies; 13+ messages in thread
From: nobu @ 2018-02-08 12:49 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by nobu (Nobuyoshi Nakada).


Since `Integer()` has `radix` optional argument, new optional argument might be confusing.
A keyword argument or a block would be better, I think.

----------------------------------------
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-70274

* Author: tenderlovemaking (Aaron Patterson)
* Status: Feedback
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:85484] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (10 preceding siblings ...)
  2018-02-08 12:49 ` [ruby-core:85478] " nobu
@ 2018-02-08 17:47 ` tom.enebo
  2018-05-29 11:35 ` [ruby-core:87296] " oliveira.matheussilva
  12 siblings, 0 replies; 13+ messages in thread
From: tom.enebo @ 2018-02-08 17:47 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by enebo (Thomas Enebo).


Two comments:

1. having block form only defeats any performance gain as executing blocks have a measurable cost.  It may be nice to have though in addition to simple nil return form.
1. it would be really nice if Ruby had some API consistency for non-exception variants of the various callswhich have made this change.  Doing each one of these as a one-off discussion almost destines these APIs to not be consistent.

----------------------------------------
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-70281

* Author: tenderlovemaking (Aaron Patterson)
* Status: Feedback
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [ruby-core:87296] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception
       [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
                   ` (11 preceding siblings ...)
  2018-02-08 17:47 ` [ruby-core:85484] " tom.enebo
@ 2018-05-29 11:35 ` oliveira.matheussilva
  12 siblings, 0 replies; 13+ messages in thread
From: oliveira.matheussilva @ 2018-05-29 11:35 UTC (permalink / raw
  To: ruby-core

Issue #12732 has been updated by m_s__santos (Matheus Silva).


rbjl (Jan Lelis) wrote:
> Although it does not solve Aaron's use case, I would suggest to have a `Integer.try_convert`, `Float.try_convert`, `Rational.try_convert`, and `Complex.try_convert` which do not raise exceptions, but just return `nil`. To keep consistency, they would just call implicit, then explicit conversion (e.g. `to_int`, then `to_i`), instead of `Integer()`'s special parsing.
> 
> It only allows strict parsing, but is much cleaner, imho. Also, it would fill some empty spots in the [core conversion table](http://idiosyncratic-ruby.com/54-try-converting.html#core-classes-conversion-table) and make Ruby's conversion logic simpler. (`.try_convert`, which currently feels more like an implementation detail, could then be embraced more).
> 
> To allow `Integer()` special conversion, it would still need an `exception:` option, but also `Float()`, `Rational()`, and `Complex()` would need it (since they currently also lack this feature due to not having a `try_convert`). One idea is to give every of the uppercased Kernel methods an `exception:` option, but this does not make sense for `Array()` and just would not be needed if going for the broader `try_convert` support).
> 
> To summarize my suggestion in two bullet points:
> 
> - Create `Integer.try_convert`, `Float.try_convert`, `Rational.try_convert`, and `Complex.try_convert` which prefer implicit conversion (if available), then explicit conversion, but return `nil` instead of raising an exception
> - Give `Integer()` an `exception` option to support special integer parsing without exceptions, but do not give `Float()` one

It would be better if `Integer.try_convert` return the conversion or the value passed if it can't convert.

----------------------------------------
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-72293

* Author: tenderlovemaking (Aaron Patterson)
* Status: Closed
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2018-05-29 11:35 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-12732.20160907051350@ruby-lang.org>
2016-09-07  5:13 ` [ruby-core:77171] [CommonRuby Feature#12732] An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception tenderlove
2016-09-07  6:55 ` [ruby-core:77180] " tenderlove
2016-10-11 11:16 ` [ruby-core:77576] " shyouhei
2016-11-25  7:01 ` [ruby-core:78307] [CommonRuby Feature#12732][Feedback] " matz
2016-11-25  9:34 ` [ruby-core:78347] [CommonRuby Feature#12732] " naruse
2016-11-28 23:01 ` [ruby-core:78405] " tenderlove
2017-02-07 11:56 ` [ruby-core:79467] " mail
2018-01-24  7:54 ` [ruby-core:85031] " matz
2018-01-24  8:00 ` [ruby-core:85032] " knu
2018-02-07 19:17 ` [ruby-core:85465] " tenderlove
2018-02-08 12:49 ` [ruby-core:85478] " nobu
2018-02-08 17:47 ` [ruby-core:85484] " tom.enebo
2018-05-29 11:35 ` [ruby-core:87296] " oliveira.matheussilva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).