在“case…when”语句(Ruby)中使用布尔运算符

时间:2022-08-22 22:54:59

I'm trying to write a method that takes the user's input and checks it for validity. It's in a "case...when" statement. This part checks that the user has entered a Y or N only.

我正在尝试编写一个方法来获取用户的输入并检查其有效性。这是在一个“案例……当“声明。这部分检查用户是否只输入了Y或N。

when 3
    input = gets.chomp.to_s.downcase
    unless input (=="y") || (=="n")
        puts "That is not a valid choice. Please enter Y or N."
        get_input(text, 3)
    end

The compiler doesn't like my boolean statement and I'm not sure why. It tells me "syntax error, unexpected [x]" and points to various parts of the statement. Pretty much tearing my hair out here... am I doing something obvious wrong?

编译器不喜欢我的布尔语句,我不知道为什么。它告诉我“语法错误,意外[x]”,并指向语句的各个部分。差不多要把我的头发都扯下来了……我做错了什么事吗?

4 个解决方案

#1


4  

Try this:

试试这个:

unless ['y', 'n'].include?(input)

#2


2  

Depending on the language(s) you come from, you might find one of these different ways more appealing. You could write:

根据你的语言,你可能会发现这些不同的方式中有一种更吸引人。你可以写:

 unless input (=="y") || (=="n")

As:

为:

if !input[/^[yn]$/]
    puts "That is not a valid choice. Please enter Y or N."
    get_input(text, 3)
end

Or:

或者:

unless input[/^[yn]$/]
  ...
end  

Or:

或者:

unless (input == 'y' || input == 'n')
  ...
end

Or:

或者:

case input
when 'y', 'n'
  ...
else
  puts "That is not a valid choice. Please enter Y or N."
  get_input(text, 3)
end

#3


1  

I find the unless first does not read well. Starting off with a negative would be fairly ok here but in more complicated conditionals it loses readability.

我发现除非一开始读得不好。在这里,从负数开始是可以的,但是在更复杂的条件下,它就失去了可读性。

I personally prefer

我个人更喜欢

$ if q !~ /[yn]/ # Does NOT match pattern
$   puts "That is not a valid choice. Please enter Y or N."
$   get_input(text, 3)    
$ end

for maximum easy readability by all.

为了最大程度的易于阅读。

#4


0  

Besides getting the condition right, there seems to be a recursion in your method. I suspect your method looks like this:

除了正确地获取条件外,您的方法中似乎还有一个递归。我怀疑你的方法是这样的:

def get_input(text, option)
  # ...
  case option
  when 1
    # ...
  when 2
    # ...
  when 3
    unless condition
      puts "That is not a valid choice. Please enter Y or N."
      get_input(text, 3) # <- recursion!
    end
  end
  # ...
end

It would be better to use a loop here. Something like this:

最好在这里使用一个循环。是这样的:

def get_input(text, option)
  # ...
  case option
  when 3
    loop do
      valid = case gets.chomp
              when /^(y|yes)$/i
                puts "positive input"
                true
              when /^(n|no)$/i
                puts "negative input"
                true
              else
                puts "That is not a valid choice. Please enter Y or N."
                false
              end
      break if valid
    end
  end
end

The input is checked with another case statement. If the input is valid (i.e. y or n), true is returned and break exits the loop, otherwise false is returned, break is not called and the loops starts over again.

用另一个case语句检查输入。如果输入是有效的(即y或n),则返回true并退出循环,否则返回false,不调用break,循环再次开始。

BTW, (y|yes) matches y and yes, (n|no) matches n and no, ^...$ ensures that there's nothing before or after, and the i makes it case insensitive.

顺便说一句,(y |是的)匹配y,是的,n(n |没有)匹配不,^……$确保在之前或之后没有任何内容,而i使它不区分大小写。

So /^(n|no)$/i matches n, N, no, No, nO, and NO.

所以/ ^(n |没有)$ /我与n,n,不,不,不,不。

#1


4  

Try this:

试试这个:

unless ['y', 'n'].include?(input)

#2


2  

Depending on the language(s) you come from, you might find one of these different ways more appealing. You could write:

根据你的语言,你可能会发现这些不同的方式中有一种更吸引人。你可以写:

 unless input (=="y") || (=="n")

As:

为:

if !input[/^[yn]$/]
    puts "That is not a valid choice. Please enter Y or N."
    get_input(text, 3)
end

Or:

或者:

unless input[/^[yn]$/]
  ...
end  

Or:

或者:

unless (input == 'y' || input == 'n')
  ...
end

Or:

或者:

case input
when 'y', 'n'
  ...
else
  puts "That is not a valid choice. Please enter Y or N."
  get_input(text, 3)
end

#3


1  

I find the unless first does not read well. Starting off with a negative would be fairly ok here but in more complicated conditionals it loses readability.

我发现除非一开始读得不好。在这里,从负数开始是可以的,但是在更复杂的条件下,它就失去了可读性。

I personally prefer

我个人更喜欢

$ if q !~ /[yn]/ # Does NOT match pattern
$   puts "That is not a valid choice. Please enter Y or N."
$   get_input(text, 3)    
$ end

for maximum easy readability by all.

为了最大程度的易于阅读。

#4


0  

Besides getting the condition right, there seems to be a recursion in your method. I suspect your method looks like this:

除了正确地获取条件外,您的方法中似乎还有一个递归。我怀疑你的方法是这样的:

def get_input(text, option)
  # ...
  case option
  when 1
    # ...
  when 2
    # ...
  when 3
    unless condition
      puts "That is not a valid choice. Please enter Y or N."
      get_input(text, 3) # <- recursion!
    end
  end
  # ...
end

It would be better to use a loop here. Something like this:

最好在这里使用一个循环。是这样的:

def get_input(text, option)
  # ...
  case option
  when 3
    loop do
      valid = case gets.chomp
              when /^(y|yes)$/i
                puts "positive input"
                true
              when /^(n|no)$/i
                puts "negative input"
                true
              else
                puts "That is not a valid choice. Please enter Y or N."
                false
              end
      break if valid
    end
  end
end

The input is checked with another case statement. If the input is valid (i.e. y or n), true is returned and break exits the loop, otherwise false is returned, break is not called and the loops starts over again.

用另一个case语句检查输入。如果输入是有效的(即y或n),则返回true并退出循环,否则返回false,不调用break,循环再次开始。

BTW, (y|yes) matches y and yes, (n|no) matches n and no, ^...$ ensures that there's nothing before or after, and the i makes it case insensitive.

顺便说一句,(y |是的)匹配y,是的,n(n |没有)匹配不,^……$确保在之前或之后没有任何内容,而i使它不区分大小写。

So /^(n|no)$/i matches n, N, no, No, nO, and NO.

所以/ ^(n |没有)$ /我与n,n,不,不,不,不。