Yield
examples/functions/yield.cr
def run puts "before" yield puts "after" end run { puts "in block" } run do puts "in do-end" end
- Optionally you can add a &anything if you think that makes the code more readable, but the important part is having yield in the code.
examples/functions/yield_block.cr
def run(&block) puts "before" yield puts "after" end run { puts "in block" }
- You can call yield more than once inside the function and the block will be executed for every yield.
examples/functions/twice.cr
def run puts "before" yield puts "middle" yield puts "after" end run { puts "in block" } puts "----" run do puts "in do-end" end