gpt4 book ai didi

ruby - 分配 begin-end-while block 的结果会阻止执行该 block

转载 作者:数据小太阳 更新时间:2023-10-29 07:59:57 25 4
gpt4 key购买 nike

您可以将 block 的结果分配给变量:

a = begin
1
end
a #=> 1

与此同时,while 可以像这样附加在 block 之后:

begin
puts "this will be printed once"
end while false

并且它至少会被执行一次。如果您为该 being-end-while block 分配一个变量,它就不会再执行:

a = begin
puts "this won't be printed at all"
end while false

谁能给我解释一下这是怎么回事?

最佳答案

如果加上赋值,则解释为:

(a = begin
puts "this won't be printed at all"
end) while false

如何在 begin ... end while false 两边加上括号?

a = (begin
puts "this won't be printed at all"
end while false)
# prints: this won't be printed at all
# a => nil

关于ruby - 分配 begin-end-while block 的结果会阻止执行该 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22660837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com