Ruby - 在一行中使用多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34276553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:47:19  来源:igfitidea点击:

Ruby - Using multiple conditions on a single line

rubyif-statementconditional-statementsmultiple-conditions

提问by Jeffrey Green

So, I'm running into this issue wherein I want to have three conditions be checked before the routine continues, but it keeps throwing up syntax errors saying it didn't expect the multiple conditions. Now, I know I've seen other people use lines such as:

所以,我遇到了这个问题,我想在例程继续之前检查三个条件,但它不断抛出语法错误,说它不希望有多个条件。现在,我知道我已经看到其他人使用诸如:

if x > 100 && x % 2 == 1
    #Do something
end

But, for whatever reason, this line:

但是,无论出于何种原因,这一行:

if (letters.eql? letters.upcase && dash.eql? '-' && numbers.to_i.to_s.eql? numbers)

is throwing up tons of errors. Is it something to do with '.eql?' or is it something extraneous about Ruby that I haven't encountered yet?

正在抛出大量错误。它与'.eql'有关吗?还是我还没有遇到过的关于 Ruby 的一些无关紧要的东西?

Here's the rest of the code for reference:

剩下的代码供参考:

print "Enter license plate: ";
input = gets.strip;
if input.length == 8
    letters = input[0,2];
    dash = input[3];
    numbers = input[4,7];
    if (letters.eql? letters.upcase && dash.eql? '-' && numbers.to_i.to_s.eql? numbers)
        puts "#{input} is a valid license plate."
    else
        print "All valid license plates are three (3) uppercase letters, followed by a dash (-), followed by four (4) digits";
    end
else
    print "All valid license plates are 8 characters long.";
end

Also, these are the errors:

此外,这些是错误:

LicensePlate.rb:7: syntax error, unexpected tSTRING_BEG, expecting ')'
...? letters.upcase && dash.eql? '-' && numbers.to_i.to_s.eql? ...
...                               ^
LicensePlate.rb:7: syntax error, unexpected tIDENTIFIER, expecting ')'
... numbers.to_i.to_s.eql? numbers)
...

回答by Rob Mulholand

Think you're just missing some parens - try this:

认为你只是缺少一些括号 - 试试这个:

if (letters.eql?(letters.upcase) && dash.eql?('-') && numbers.to_i.to_s.eql?(numbers))

if (letters.eql?(letters.upcase) && dash.eql?('-') && numbers.to_i.to_s.eql?(numbers))

回答by Ryan Rebo

This should do it:

这应该这样做:

if letters.eql?(letters.upcase) && dash.eql?('-') && numbers.to_i.to_s.eql?(numbers)

if letters.eql?(letters.upcase) && dash.eql?('-') && numbers.to_i.to_s.eql?(numbers)

You can still wrap the entire conditional in parenthesis if you would like, but with Ruby (unlike JavaScript), you don't need to.

如果您愿意,您仍然可以将整个条件用括号括起来,但是对于 Ruby(与 JavaScript 不同),您不需要这样做。

回答by Ohad Dahan

This also works:

这也有效:

letters.eql? letters.upcase and dash.eql? '-' and numbers.to_i.to_s.eql? numbers

I believe this is due to operator precedence since this also works:

我相信这是由于运算符优先级,因为这也有效:

(letters.eql? letters.upcase) && (dash.eql? '-') && (numbers.to_i.to_s.eql? numbers)

Ruby seem to try and evaluate your condition prematurely.

Ruby 似乎过早地尝试评估您的状况。

EDIT: Just saw that Lurker was mentioning precedence previously.

编辑:刚刚看到 Lurker 之前提到了优先级。

回答by Stefan

In addition to the other answers - consider using a regular expressionto check the format:

除了其他答案 - 考虑使用正则表达式来检查格式:

print "Enter license plate: "
input = gets.chomp
if input.length != 8
  puts "All valid license plates are 8 characters long."
elsif input !~ /^[A-Z]{3}-\d{4}$/
  print "All valid license plates are three (3) uppercase letters, followed by a dash (-), followed by four (4) digits"
else
  puts "#{input} is a valid license plate."
end