Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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