...So you know, gets.chomp
doesn't return an array. It returns a String. Something like this will give you the array you want:
array = gets.chomp.split(/\D/).map { |e| e.to_i }
That converts it from a single string (which happens to contain ,
-separated values) to an array of numbers.
puts 'The array you entered was invalid!' if array.any? { |item| !(1..10).include?(item) }
That goes through and checks if any
of the values return true for !(1..10).include?(item)
, which returns true if and only if the range [1,10]
(inclusive) contains item
. If so, it prints out The array you entered was invalid!
.
However, it looks like what you want to do is physically prevent the user from entering a number like 11
or 12
into the console, which (in pure Ruby at least) is impossible. The closest you can get is validating the input after the fact, which is what this does. Look into Ruby's various loops if you want to have them keep entering the array until the array they enter is valid.
Note that @locoboy's answer will work for single numbers, but it will fail when attempting to validate the entire array, or the direct result of gets.chomp
.