Why do do...end and curly braces behave differently with method arguments?
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:
Cite
Poignant 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.numbers = [1, 2, 3] puts numbers.map { |n| n * 2 } puts numbers.map do |n| n * 2 endOutput 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
mapwithout a block returns an Enumerator, so something is callingmapwith 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.
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
2 answers
- 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 writeputs(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.