Blocks
General Syntax:
Blocks are a set of code that explains what to do. It is nothing more than a set of instructions. Blocks are one of the only things in Ruby that is not an object. Since blocks are not objects, they can not be stored to variables and don’t have all the powers that other objects have. Blocks are similar to anonymous functions in JavaScript and lambdas in Python. Blocks can be defined with either the key words: do
and end
, or with curly braces { }
.
For example:
or
Passing Blocks to Methods:
Passing a block to a method is a great way of abstracting certain tasks from the method and defining those tasks when we call a method. Abstraction is an important idea in computer science, and you can think of it as meaning "making something simpler.”
For example:
Whenever the each
method is called, it is passed a block as a parameter, with a set of instructions as to what needs to be done.
Last updated