run what block defines
ex:
def test(*args, &block)
yield(*args)
end
test("hello"){ |i| puts i }
---> hello
note:
yield(*args) = block.call(*args)
when defining a method that receives blocks,
there are two ways
(1) define method with block arguments
ex:
def test( &block)
end
(2)define method without block arguments
ex:
def test
end
pass arguments to blocks
in the method definition, call yield with arguemtns
ex:
yield (a, b, c)
in the block, use | | to receive arguments
ex:
| a, b, c|
return value from call block
the return value is last expression evaluated in block
ex:
in the method definition
a= yield
沒有留言:
張貼留言