Crystal by example: Blocks
Blocks are pieces of code that you pass to methods, allowing the method to execute that code whenever it wants. They can receive arguments and return values, functioning like anonymous functions.
yield is the way a method calls a block passed to it, and it is necessary for blocks to be executed.
Blocks have two syntaxes: one using braces {} and another using do and end, each having its own scope.
Blocks accept parameters, which can have specified types.
def print_block puts yieldend
print_block do "do end"end
print_block { "New syntax" }
def greet(name) yield nameend
greet "Ricardo" do |name| puts nameend
greet "Carl" { |name| puts name }
def number_operation(a, b) return if a.zero? || b.zero? yield(a, b)end
sum = number_operation 5, 3 do |a, b| a + bend
puts sum
def repeat(n : Int32) n.times do yield endend
repeat 5 do puts "I'm repeating"endcrystal run blocks.cr
do endNew syntaxRicardoCarl8I'm repeatingI'm repeatingI'm repeatingI'm repeatingI'm repeating Next example: Procs