Ruby BLOCKS Explained 101
Block is basically anonymous method that gets called from another method. If you have some code that executes passed in method like this:
You can write it like this using blocks:def execute_method_in_block(named_method)
named_method.call
named_method.call
end
execute_method_in_block Proc.new {puts "Hello, blocks are cool!"}
def execute_method_in_block
yield
yield
end
execute_method_in_block {puts "Hello, blocks are cool!"}
We’ve lost “Proc.new” and a method name, we are just saying
Whenever you find yield, execute block that is defined within braces.
You might be wondering what’s the fuss all about, (more…)