Ruby 什么时候需要 do 关键字? [英] When is the do keyword required in Ruby?

查看:32
本文介绍了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:

whileuntil 循环中的 do 关键字类似于if 语句:只要换行(或分号)出现在循环条件和循环体之间.

The do keyword in a while or until loop is like the then keyword in an if 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆