Block Basics (Review)
In this page:
Block Basics (Review)
A block is the code attached to a method call with { } or do...end, and it isn't an object by itself -- it only exists as part of that specific call, invoked via yield. This tutorial revisits blocks specifically to contrast them with Proc and Lambda, which turn similar logic into a reusable, storable object. Understanding this distinction is key to picking the right tool for a given situation.
Note: A bare block can't be stored in a variable or passed around on its own -- for that, you need a Proc or Lambda.
Example: Block Basics (Review)
def run_twice
yield
yield
end
run_twice { puts "Running from a block" }
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: