Ruby 代码效率

Ruby code efficiency

有没有办法让这段代码更短更简单?

loop do 
    if possibleSet.split(" ").map(&:to_i).any? {|e| (e<0 || e>12)}
        print "Please enter valid numbers (between 1 and 12): "
        possibleSet = gets
        errorinput = false
    else
        errorinput = true
    end
    break if errorinput
end

这会解析数组中的用户输入(行为不完全相同,但我希望它更简洁,您可以从那里开始工作)

set = []
until set.all? {|i| (1..11).include?(i) } && !set.empty? do
  set = gets.split(' ').map(&:to_i)
end

重构了一下:)

loop do 
    print "Please enter valid numbers (between 1 and 12): "
    possibleSet = gets.chomp
    break unless possibleSet.split(" ").map(&:to_i).any? {|e| (e<0 || e>12)}
   end

下面的代码将检查输入的正确性:

input = loop do
  print "Please enter valid numbers (between 1 and 12): "
                          # ⇓⇓⇓ as many spaces as user wants
  input = gets.chomp.split(/\s+/).map(&:to_i) rescue []
  break input unless input.empty? || input.any? { |i| !(0..12).include? i }
end