Your problem is in this line:
if accountant == (0..9)
This is checking whether the value is equal to a range - which is not what you wanted.
Since the input from gets.chomp
will always be a string, you need to check whether it only contains the characters: "0"
, "1"
, "2"
, ... and "9"
.
One way to do this is with a regular expression:
if accountant =~ /\A\d+\z/
\A
means "start of string"
\z
means "end of string"
\d+
means "one or more digit" (0-9)
Note that the solution you suggested in the comments, /^-?[0-9]+$/
, is flawed since:
^
means "start of line" (so it would be possible to insert arbitrary other characters before a newline)
$
means "end of line" (so it would be possible to insert arbitrary other characters after a newline)
-?
also allows an optional hyphen character. Which is presumably not what you want in this context, since the input is a salary - which surely cannot be negative!