Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby determine if a string only contains numbers

I am trying to create a method that asks for the user to create a pin number, like a bank account pin. It can only be 4 numbers in length, and every character entered must be numbers. I have found when I use this code:

puts "Please give us a 4 digit pin number for your account:"
@response = gets.chomp 

unless @response.to_i.is_a?(Integer) && @response.to_str.length == 4
    puts "Your response must be 4 numbers in length."
    create_pin
else
    @pin = @response.to_i
    puts "Your pin has been set."
end

It does not work as I would prefer, because the .to_i method will gladly convert '2134a' to 2134. As well, if there is no Integer in the string, it records the value as 0, which is not false as I need it to be.

Test if a string is basically an integer in quotes using Ruby?

I found a really helpful post, but it seems to get quite complicated in the thread do to regexp. This post was also shut down because the creator was not specific enough:

Check if string contains only positive numbers in Ruby

So I'll try be clear, I am trying to limit the user input so they can only type numbers 0 through 9. Math equations like 10^3 need to be fail, as well as negative numbers like -1234, and I'm trying to limit them to four digits the way any pin would be limited. So far, I'm completely stumped and haven't seen a way to do it that doesn't contain a whole bunch of regexp.

Perhaps I could check if the string contains a list of unacceptable characters.

Thanks ahead.

Edited for better phrasing and to correct an error I wrote in the second paragraph.

like image 975
Mark H Jr Avatar asked Aug 03 '26 14:08

Mark H Jr


1 Answers

Try this:

def valid_pin?(pin)
  /^\d{4}$/ === pin
end

What this is saying basically is:

  1. /^d{4}$/ is a regular expression, you can tell because it is enclosed in / / with a pattern in the middle
  2. the ^ and $ characters denote the beginning and end of your string, respectively. Basically what is ensures is that strings with 4 consecutive numbers but which have other characters at the beginning or end (i.e. "a1234" and "5678b") will not be accepted.
  3. \d is the regular expression character denoting digits. When followed by {4}, this means to look for exactly 4 digits. Ruby also allows you to specify a minimum value using this notation {3,} or a range {3,6}.
  4. The === method (as correctly mentioned by @iamnotmaynard), sometimes called the threequals operator, returns a boolean (true or false) value depending on whether or not the regular expression matches the given string.

In the context of your code:

puts "Please give us a 4 digit pin number for your account:"
@response = gets.chomp 

unless valid_pin?(@response)
  puts "Your response must be 4 numbers in length."
  create_pin
else
  @pin = @response.to_i
  puts "Your pin has been set."
end

If you want to try and learn about Regular Expressions (commonly referred to as RegExes), I would encourage you to play around on the awesome site Rubular, there is a general keyword list and RegEx sandbox to help you text your creations.

like image 140
wvandaal Avatar answered Aug 05 '26 10:08

wvandaal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!