[英]Backslash in Ruby conditional statement
我正在浏览一些代码并发现了这一点:
stem = ""
answer = ""
return if stem.nil? || answer.nil? || \
stem.question == answer.question
\\
是什么? 我知道\\
用于字符串中,但是我以前从未见过用例。 这是语法错误还是某些高级的ruby语法? 我想念什么吗?
这是一个无用的换行符。
问题“这是语法错误”似乎很容易发现。
\\
用于指示红宝石中的行连续性。 使用\\
将去除\\n
(换行符)字符。
示例(使用字符串):
没有\\
:
2.1.2-perf :018 > s = "test this
2.1.2-perf :019"> out"
=> "test this\nout"
用\\
:
2.1.2-perf :020 > s = "test this \
2.1.2-perf :021"> out"
=> "test this out"
示例(不使用字符串):
没有\\
:
2.1.2-perf :043 > return "test" if true && false &&
2.1.2-perf :044 > true
=> nil
用\\
:
2.1.2-perf :045 > return "test" if true && false && \
2.1.2-perf :046 > true
=> nil
在您的情况下,这无关紧要,但这不是语法错误。
从文档 :
Ruby程序是表达式序列。 每个表达式都用分号(;)或换行符分隔。 行尾的反斜杠不会终止表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.