Ruby:多行条件语法:我该怎么做?

标签 ruby syntax

我正在尝试做的事情:

result = (not question?) \
          and ( \
            condition \
            or ( \
              comparer == compared and another_question? \ 
            ) \
          )   

目标是在拥有复杂和/或逻辑的同时仍然具有可读性。

上面尝试的语法的问题是它在某种程度上弄乱了 ruby​​ 解析器中的括号,所以控制台说错误出在一个文件中,而这个代码不在其中。(尽管它在调用堆栈中)

没有反斜杠,我得到这些:

syntax error, unexpected kAND, expecting kEND (SyntaxError)

 syntax error, unexpected kOR, expecting ')'

关于如何正确执行此操作的任何想法?

最佳答案

删除 another_question 中反斜杠后的空格?\。您正在转义空格而不是换行符,这会导致语法错误。

请注意,您不需要转义每个换行符。

result = (not question?) \
          and (
            condition \
            or (
              comparer == compared and another_question?
            )
          ) 

关于Ruby:多行条件语法:我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7210794/

相关文章:

javascript - 使用 JS 和 Keyup 在 Rails 4 中进行实时搜索

编码恐怖或绝妙想法的 C++ 示例?

c++ - 我的 C++ 代码给出了在代码中看不到的错误。错误是什么?

c++ - "({ expr1; expr2; })"是如何用C++编译的?

bash - '$?' 在 bash 脚本中意味着什么?

ruby - 是否可以在 proc 中查看 ruby​​ 代码?

ruby - 错误 : Gem bundler is not installed, 首先运行 `gem install bundler`

php - 翻译 phpmyadmin 语法 table.field

Ruby 数组方法 array.each.upcase

ruby-on-rails - Arel,如何加入