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 twiceResult
hi hi no block
Exercise
Run the block.
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.