Skip to content
Justice Tech School

Blocks and yield

A block is code passed to a method. yield runs it and block_given? tests for one.

Example
def twice
  return "no block" unless block_given?
  yield
  yield
end
twice { puts "hi" }
puts twice

Result

hi
hi
no block

Exercise

Run the block.

Quiz Test

How do you test for a block?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.