For web programming, numbers come in as strings. but to_i will convert \"5abc\" to 5 and \"abc\" to 0, both
to_i
\"5abc\"
5
\"abc\"
0
Use a simple regex to check str is an integer.
str
def number_or_nil(str) str.to_i if str[/^-?\d+$/] and str.line.size == 1 end