Karen
Karen

Reputation: 195

how to check if user input contains a number? in Ruby

I don't want to see if user input is an integer value, I just want to see if there's an integer within the user's input (a string).

I was thinking of using include? or contains but neither worked for me.

Upvotes: 0

Views: 142

Answers (1)

tadman
tadman

Reputation: 211540

Lazy solution:

input = gets.chomp
input[/\d+/]

You can also check with match? on Ruby 2.6+:

input.match?(/\d+/)

Upvotes: 3

Related Questions