4

I'm trying to validate that the input a user gives my program via gets is an integer. is_a?(Integer) does not work, as far as i can tell, because gets gets a string from the user, so it will always return false even if the user enters an valid integer (in string form). One would think I could simply use to_i on the input and be done with it, but that raises another issue - "75akjfas".to_i results in 75. So if I relied on to_i to solve my problems, anything starting with numbers will work.

How do I cleanly validate that the value is an integer only, and not a combination of numbers and letters? Or do I need to resort to regex for this?

1

6 Answers 6

7
print "Enter an integer (or try to sneak by something other): "
puts Integer(gets) rescue puts "Hey, that's not an integer!"
Sign up to request clarification or add additional context in comments.

1 Comment

I'd just add a quote from the docs: "strings should be strictly conformed to numeric representation. This behavior is different from that of String#to_i"
6

How about s.to_i.to_s == s? I'd prefer regex however.

1 Comment

i have no reason to not use regexp. i'm just hoping there's a nice helper function somewhere that i'm missing :).
1

Using regex you could do it like this:

class String
  def integer?
   !!(self =~ /^[-+]?[0-9]+$/)
  end
end

Comments

1

You could use Integer() but you need to be careful with hex, binary, or octal input. http://ruby-doc.org/core-2.3.1/Kernel.html#method-i-Integer

def valid_integer?(string)
  begin
    !!Integer(string)
  rescue ArgumentError
    false
  end
end

Comments

0

Check this code example for how to use the checked string input by the user
puts "Enter a number: " user_input = gets.chomp check = (user_input.to_i.to_s == user_input)

while (!check ) do
 puts("Wrong Input, Pls, Enter a number: " )
 user_input = gets.chomp
  check = (user_input.to_i.to_s == user_input)
end

if user_input.to_i < 0
puts "Number is negative"
elsif user_input.to_i > 0
puts "Number is positve"
else
puts "Number is Zero"
end

2 Comments

The link you have provided yields an application error. Maybe that is just a passing circumstance, but consider whether it is a lasting one you need to correct. Also, the connection between the link you have provided and the sample code in your answer is not very clear. Are they the same? Edit your answer to make it clear.
thanks @J0e3gan i have removed the link and it does show the same thing just that you can be able to run it there... I noticed the link was private to me.
-2

Ruby can do it without esoteric solutions:

Integer is a integer

1970.is_a?Integer
true

String is not a integer

"1970".is_a?Integer
false

String to integer is a integer

"1970".to_i.is_a?Integer
true

2 Comments

This seems not to be totally correct. @LyleDickie wrote this as a suggested edit: Unfortunately the "String to integer is a integer" section is incorrect. If you run it in IRB it does return true. However, if the user were to enter "197e" it would also evaluate to true. ``` "197e".to_i.is_a?Integer true ```
"a".to_i is 0. Which is still an integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.