Project

General

Profile

Actions

Feature #12455

open

Add a way for class String to determine whether it has only numbers / digits or not

Added by shevegen (Robert A. Heiler) over 8 years ago. Updated over 8 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:75830]

Description

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!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0