我想要做的是定义一个方法来获取块中每个方法的结果(或调用块中的每个方法)并返回结果字符串......
例如:
def a_method(&block)
## build a string by calling each of the blocks methods
end
def brackets(str)
'('+str+')'
end
a_method do
brackets "foo"
brackets "bar"
end
# desired output => (foo)(bar)
我尝试yield
输入a_method
,但正如我所料,它只返回块中最后一个评估的表达式(在本例中为(bar)
)。
我已经查看了如何从块内的调用构建字符串,但找不到任何东西。我原以为这将是一种相当普遍的技术..有人可以解释一下吗?