Ruby 什么时候需要 do 关键字? [英] When is the do keyword required in Ruby?
本文介绍了Ruby 什么时候需要 do 关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,下面代码中do
的存在与否对程序的行为有影响吗?
For example, does the presence or absence of do
in the following code affect the behavior of the program at all?
while true do
puts "Hi"
break
end
while true
puts "Hi"
break
end
推荐答案
根据 The Ruby Programming Language 书第 5.2.1 节:
According to The Ruby Programming Language book Section 5.2.1:
while
或 until
循环中的 do
关键字类似于if
语句:只要换行(或分号)出现在循环条件和循环体之间.
The
do
keyword in awhile
oruntil
loop is like thethen
keyword in anif
statement: it may be omitted altogether as long as a newline (or semicolon) appears between the loop condition and the loop body.
所以,不,它不会改变行为,它只是可选的语法.
So, no, it won't change the behavior, it's just optional syntax.
这篇关于Ruby 什么时候需要 do 关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文