-2

I am trying to define an array based on user input and want to make sure that each value is between 1 and 10. How can I do that?

So I am using this right now.

array = gets.chomp

I want the user to only input values ranging from 1 to 10. How do I do that?

Shaili Parikh
  • 107
  • 1
  • 3
  • 11
  • http://ruby-doc.org/core-2.2.0/Range.html – Nic Jun 03 '15 at 20:40
  • Or, if you meant something other than defining an array ranging from 1 to 10, clarify your quesiton. – Nic Jun 03 '15 at 20:40
  • @QPaysTaxes I just want to define an array having values from 1 to 10 but can be repeated as many times as wanted. How do I do that? – Shaili Parikh Jun 03 '15 at 20:47
  • possible duplicate of [Correct way to populate an Array with a Range in Ruby](http://stackoverflow.com/questions/191329/correct-way-to-populate-an-array-with-a-range-in-ruby) – Rots Jun 03 '15 at 20:53

2 Answers2

1

You can just use the below to do that:

def verify(input)
    (1..10).to_a.include?(input)
end

#input is user input
if verify(input)
  #do some stuff
else
  #don't do stuff
end
locoboy
  • 38,002
  • 70
  • 184
  • 260
0

...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.

Nic
  • 6,211
  • 10
  • 46
  • 69
  • @ShailiParikh No, it works with an array of numbers. Please add the input format to your question if you'd like help turning that into an array of numbers as well. – Nic Jun 03 '15 at 20:56
  • Okay, so I basically have to have all my values ranging from 1 to 10 and the user will input those values. Is there no way I can make sure that the user does not input 11 or 12 or anything greater than 10 – Shaili Parikh Jun 03 '15 at 20:57
  • @ShailiParikh That's not what I asked. In what format will the numbers be entered? Into the standard input, with each number on a new line? One long line of numbers, with each separated by some character? _Explain what you want_. Ideally in the question itself, with examples. – Nic Jun 03 '15 at 20:59
  • right, so I can put an error statement until the values are entered within my range. Can I do if? – Shaili Parikh Jun 03 '15 at 21:27
  • @ShailiParikh I'm sure I could answer that if you rephrased it so it made sense. – Nic Jun 03 '15 at 21:30
  • My bad. I am just asking if I can add an if..els statement verifying the values in the array to be in the required range – Shaili Parikh Jun 03 '15 at 21:34
  • @ShailiParikh That you can. You'd put the first bit of code first, to define `array`, then everything after `if` in the second as your condition. – Nic Jun 03 '15 at 22:02