Issue #12455 has been updated by Nobuyoshi Nakada.


I feel that `to_i` extension might be better than `Integer`.

```ruby
"foo".to_i                #=> 0
"foo".to_i(fallback: nil) #=> nil
```

----------------------------------------
Feature #12455: Add a way for class String to determine whether it has only numbers / digits or not
https://bugs.ruby-lang.org/issues/12455#change-59712

* Author: Robert A. Heiler
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Hello.

Not sure if that has been suggested before. If it was and not deemed fit,
please feel free to close this issue request here.

For class String, would it be possible to find out if a given String
contains only numbers, without having to do a regex check?

    x = '123'

A regex will do of course:

    if x =~ /^\d+$/

I was wondering if we could have a method that does something similar,
that is also easier to read.

Something like:

    x = '123'
    x.only_numbers?

or

    x.only_digits?

Something like that. 

I do not know whether the word "only" is good or not. Perhaps ".all_numbers?"
or ".all_digits?" would be better. But the name is secondary probably, more
important would be whether such a method would make sense for class String.

I am more interested in a method that will help avoid the use of a regex here.

The method should return true if the string has only numbers, and false
otherwise.

Anyway, thanks for reading!



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