Skip to content
Poignant Guide Ruby Q&A

Why do do...end and curly braces behave differently with method arguments?

Asked by Yara H. · Blocks, procs and iterators2 repliesupdated

Short answer

I was sure do ... end and { ... } were two spellings of the same thing, like begin/end and braces in C. Then this happened in a script for my data structures course:

Written by Yara H. ·
CitePoignant Guide Ruby Q&A, "Why do do...end and curly braces behave differently with method arguments?", https://poignantguide.net/answers/read/why-do-doend-and-curly-braces-behave-differently-with-method-arguments/, accessed 2026-12-01.
  1. Member ·
    numbers = [1, 2, 3]
    
    puts numbers.map { |n| n * 2 }
    puts numbers.map do |n| n * 2 end
    

    Output on Ruby 3.3.4:

    2
    4
    6
    #<Enumerator: [1, 2, 3]:map>
    

    The first line does what I expected. The second prints an Enumerator and the doubled numbers never appear. No error, no warning. The block is the same text, only the delimiters differ.

    I know map without a block returns an Enumerator, so something is calling map with no block. But I wrote the block right next to it. Is this a parser quirk, or is it documented? And is there a rule for which one to use? I read the guide's chapter 4 and it uses both styles without explanation.

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. Sunita R.Member ·

    Coming from Python, the thing I noticed is that Ruby lets you skip the parentheses on puts, and that is what causes the trouble. If you write puts(numbers.map do |n| n * 2 end) the output is 2, 4, 6 again. I would like to know if that is the recommended fix or only a workaround.

  2. Kai N.Member ·

    They are identical, it is only style. Braces for one line, do/end for several. If you get the Enumerator, you probably have a typo somewhere else in the file, or the data is not what you think it is. Print numbers first.

Poignant Guide Ruby Q&A. Questions and answers on learning Ruby and programming: syntax, blocks, objects, gems, Rails, debugging and reading the Poignant Guide.