Blocks Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the difference between curly braces and Do End blocks?

A

Curly braces block has higher precedence than Do-End blocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a closure in Ruby?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does the & key work when dealing with blocks?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

For these codes:
- foo bar do puts ‘Hi’ end
- foo bar { puts ‘Hi’ }
In which order the blocks are called?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly