Blocks, Procs, and the "&" Bridge (opens in new tab)
&blk in a parameter list turns a block into a Proc; &proc at a call turns a Proc back into a block. 1. A block is syntax, not an object A block is the { ... } or do ... end you attach to a method call. It is not a value you can hold by itself. You can't store a block in a variable — Ruby won't even parse it: b = { |x| x * 2 } # SyntaxError # Ruby reads { } here as a hash, not a block. A method can carry at most one block: [1, 2, 3].each { |x| puts x } # fine — exactly one block # There is no ...
Read the original article