print "What is your zip code? "
zip_code = gets.chomp
puts zip_code
zip_code_array = zip_code.split
unless zip_code_array.length == 5
puts "Error"
else
puts "Good"
end
A ZIP code is not an integer. It's a string of five digits.* For example, 00911
is a valid ZIP code (San Juan, Puerto Rico), but it is not an integer in any practical sense.
The correct solution, then, is not to check if the string is an integer of a certain size, but rather if it's a string of five digits.
A regular expression will make short work of this:
print "What is your zip code? "
zip_code = gets.chomp
puts zip_code
if zip_code =~ /^[0-9]{5}$/
puts "Good"
else
puts "Error"
end
In case you're not familiar with regular expressions, this one breaks down like so:
/
^ # beginning of line
[0-9] # a digit 0-9
{5} # repeated exactly 5 times
$ # end of line
/x
*Strictly speaking, a ZIP code can be five digits or five digits followed by a dash and four digits (ZIP+4). You'd be wise to accommodate both in your application.