是否开始...结束 while 表示一个“块"? [英] Does begin . . . end while denote a 'block'?
问题描述
temp = 98.3
begin
print "Your temperature is " + temp.to_s + " Fahrenheit. "
puts "I think you're okay."
temp += 0.1
end while temp < 98.6
在上面的例子中,begin 和 end 之间的所有东西都是一个块吗?
In the above example, is everything between begin and end a block?
我仍然对块是什么感到困惑.
I'm still confused what a block is.
如果你不能称它为一个块,你会称它开始和结束之间的那段代码是什么?可以称之为块吗?
If you can't call it a block, what would you call that chunk of code between begin and end? Is it ok to call it a chunk?
推荐答案
Block 在 Ruby 中有特殊的含义.根据 Ruby 的创建者 Matz 的说法,您可以将块视为无名函数 - 通常可以被yield
输入的东西,也可以取参数.
Block has a special meaning in Ruby. According to Matz, Ruby's creator, you can look at a block as a nameless function - typically something that can be yield
ed into, and which may also take parameters.
在描述 Ruby 语法时,您可能会看到以下类型的歧义:
You may see the following kind of disamiguation when describing Ruby syntax:
begin
...end
(在其他语言中称为块)有时可以简单地称为它是什么,即表达式(可能在转包含其他表达式 - 表达式只是具有返回值的东西)在 Ruby 中.一些参考文献仍将其称为begin/end block
,或code block
,增加了一些混乱do
...end
或{...}
将始终被称为 阻止在 Ruby 中
begin
...end
(what is called block in other languages) may sometimes be referred to simply as what it is, i.e. an expression (which may in turn contain other expressions - an expression is simply something that has a return value) in Ruby. Some references will still call it abegin/end block
, or acode block
, adding somewhat to the confusiondo
...end
or{...}
will always be referred to as a block in Ruby
例如,仔细阅读Ruby 语法手册页,例如
begin
表达式
end
表达式
while
表达式
loop
block
begin
expression
end
expression
while
expression
loop
block
如需进一步阅读,请参阅:
For further reading, see:
这篇关于是否开始...结束 while 表示一个“块"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!