Bird
0
0

Why might a Ruby developer choose do..end over curly braces {} for a block, beyond style preferences?

hard📝 Conceptual Q10 of 15
Ruby - Blocks, Procs, and Lambdas
Why might a Ruby developer choose do..end over curly braces {} for a block, beyond style preferences?
ACurly braces cannot accept block parameters.
B<code>do..end</code> blocks run faster than curly braces.
C<code>do..end</code> has lower precedence than curly braces, affecting method chaining.
D<code>do..end</code> blocks cannot be passed to methods.
Step-by-Step Solution
Solution:
  1. Step 1: Understand block precedence in Ruby

    Curly braces have higher precedence than do..end, which can affect how expressions are parsed, especially with method chaining.
  2. Step 2: Evaluate options

    do..end has lower precedence than curly braces, affecting method chaining. correctly states the precedence difference. Options A, B, and D are false because performance is similar, curly braces accept parameters, and both block types can be passed to methods.
  3. Final Answer:

    do..end has lower precedence than curly braces, affecting method chaining. -> Option C
  4. Quick Check:

    Block precedence difference = C [OK]
Quick Trick: do..end has lower precedence than {} in Ruby [OK]
Common Mistakes:
  • Thinking do..end is faster
  • Believing curly braces can't take parameters
  • Assuming do..end can't be passed as block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes