Check If A String Only Contains A Number In Ruby

Posted by Weston Ganger

March 21, 2016

I was importing string values and needed to check that it was a positive number

Heres a simple regex to check this. This example will only work for positive integers.

"123abc" !~ /\D/ # => false
"123" !~ /\D/ # => true

Related External Links:

Posted in Rails and Tagged with rubyrails

Recommended Posts



Westonganger profile

Send Me A Message

I would love to get in touch and talk about your next project. Feel free to send a message and I will get back to you shortly.

Get Connected