我尝试使用此问题中的代码
def call(&block)
block.call(3, "test")
end
call do |x, y|
puts x
{x, y}
end
但只得到错误:
块参数数错误(给定 2,预期为 0(
可以吗,也许还有其他方法可以调用块?
游乐场链接: https://play.crystal-lang.org/#/r/4t8h
Github问题:https://github.com/crystal-lang/crystal/issues/6597
您可以使用以下两种形式之一:
def call(&block : Int32, String -> {Int32, String})
block.call(3, "test")
end
result = call do |x, y|
{x, y}
end
result # => {3, "test"}
或
def call
yield 3, "test"
end
result = call do |x, y|
{x, y}
end
result # => {3, "test"}
可以在此处找到一些其他信息。