Yield with parameters
def run
puts "before"
yield 42
puts "after"
end
run { |value|
puts "in block #{value}"
}
puts "----"
run do |value|
puts "in do-end #{value}"
end
puts "----"
run {
puts "in block"
}
puts "----"
run do
puts "in do-end"
end
- This example based on the example on the Crystal web site does not work:
def foo
puts "foo before"
yield
puts "foo after"
end
def bar
puts "bar before"
yield
puts "bar after"
end
foo bar {
puts "in block"
}
puts "----"
foo bar do
puts "in do-end"
end