簡體   English   中英

Ruby條件語句中的反斜杠

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM