Ruby: 隐式块转换为 Proc
Ruby: Implicit Block Converted to Proc
我的理解是必须生成隐式附加到方法的块;它不能被调用。所以我试图理解为什么会这样:
def execute_code
proc.call
end
execute_code { "Why does this work?" } # => "Why does this work?"
将块附加到此代码成功执行。
有什么见解吗?我还没有找到任何文档暗示隐式块会自动转换为 proc 对象并分配给变量 proc
.
Ruby 2.5.3
对于 Ruby 2.5.3,docs for Kernel#proc()
表示:
Equivalent to Proc.new.
和 docs for Proc.new
说:
Creates a new Proc
object, bound to the current context. Proc::new
may be called without a block only within a method with an attached block, in which case that block is converted to the Proc
object.
这就是您的示例中发生的情况。您在带有块的方法中调用 proc
,并且该块正在转换为 Proc.
但是,此行为在更高版本中发生了变化。如果你在 Ruby 2.7,1 中尝试,你会收到这样的警告(尽管它仍然有效):
proc.rb:2: warning: Capturing the given block using Kernel#proc is deprecated; use `&block` instead
在 Ruby 3 中,它根本不起作用(实际上表现如您所料):
proc.rb:2:in `proc': tried to create Proc object without a block (ArgumentError)
from proc.rb:2:in `execute_code'
from proc.rb:5:in `<main>'
docs for 3.0.0 are unchanged though. This looks like a bug in the docs (it has been fixed in master). It looks like this was first raised in the issue tracker in 2014 and then later in 2019.
我的理解是必须生成隐式附加到方法的块;它不能被调用。所以我试图理解为什么会这样:
def execute_code
proc.call
end
execute_code { "Why does this work?" } # => "Why does this work?"
将块附加到此代码成功执行。
有什么见解吗?我还没有找到任何文档暗示隐式块会自动转换为 proc 对象并分配给变量 proc
.
Ruby 2.5.3
对于 Ruby 2.5.3,docs for Kernel#proc()
表示:
Equivalent to Proc.new.
和 docs for Proc.new
说:
Creates a new
Proc
object, bound to the current context.Proc::new
may be called without a block only within a method with an attached block, in which case that block is converted to theProc
object.
这就是您的示例中发生的情况。您在带有块的方法中调用 proc
,并且该块正在转换为 Proc.
但是,此行为在更高版本中发生了变化。如果你在 Ruby 2.7,1 中尝试,你会收到这样的警告(尽管它仍然有效):
proc.rb:2: warning: Capturing the given block using Kernel#proc is deprecated; use `&block` instead
在 Ruby 3 中,它根本不起作用(实际上表现如您所料):
proc.rb:2:in `proc': tried to create Proc object without a block (ArgumentError)
from proc.rb:2:in `execute_code'
from proc.rb:5:in `<main>'
docs for 3.0.0 are unchanged though. This looks like a bug in the docs (it has been fixed in master). It looks like this was first raised in the issue tracker in 2014 and then later in 2019.