Blocks Flashcards
What is the difference between curly braces and Do End blocks?
Curly braces block has higher precedence than Do-End blocks.
What is a closure in Ruby?
It is the name given to a function that can store, that is, to enclose the values of local variables within the scope in which the block was created.
How does the & key work when dealing with blocks?
When an & is added to an argument in a method it means that the method is expecting a block to be sent in that argument. When we call the method we must add the & in the parameter too.
For these codes:
- foo bar do puts ‘Hi’ end
- foo bar { puts ‘Hi’ }
In which order the blocks are called?
In the first code, do-end will be called for the foo method.
In the second code, curly braces will be called for the bar method. We can use parentheses to clarify for what method we want to call the block.