Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7023627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:44:06+00:00 2026-05-27T23:44:06+00:00

Any thoughts on why the following code outputs the contents of the {bracket} code

  • 0

Any thoughts on why the following code outputs the contents of the {bracket} code but not the “do” code?

my_array = ["alpha", "beta", "gamma"]
puts my_array.collect {
    |word|
    word.capitalize
}
puts "======================"
puts my_array.collect do |word| word.capitalize end
puts "=========END=========="

When executed the code returns the following as the output

Alpha
Beta
Gamma
======================
#<Enumerator:0x2517ed0>
======================

Any and all help appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:44:06+00:00Added an answer on May 27, 2026 at 11:44 pm

    do ... end and {} block syntaxes have different precedence – a block defined using braces binds tighter to its caller than one using do ... end, which is why your first example works as intended.

    Edit: Elaborating a bit: When you use puts my_array.collect {|word| word.capitalize}, you’re sending to puts the result of my_array.collect {|word| word.capitalize} – the array resulting from capitalizing each item in the original array. When you use puts my_array.collect do |word| word.capitalize end, you’re passing the value of my_array.collect – an enumerator – to puts, and ALSO passing the block to puts. Since puts doesn’t yield to the block, you end up writing the string representation of your enumerator to $stdout, and the block never gets called. Ergo, it’s a good idea to use parenthesis when using blocks like this, unless you (and anyone you’re working on the code with!) knows exactly what’s happening.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting the following error when trying to run reports. any thoughts or
Anyone have any thoughts on how/if it is possible to integrate Google Code commits
Any thoughts on using Flex to build an RIA for administering a complex rails
Any thoughts on why this isn't working? I really thought 'ignore' would do the
Does anyone have any thoughts on this method? I have did some performance testing
Exactly what the title says...........any thoughts on other good options for relational database implementation
Just wondering if you had any thoughts on this problem. I want to make
I've been researching memory mapped files for a project and would appreciate any thoughts
Visual Studio randomly crashes when adding/removing references and projects. Any thoughts why? Will installing
What are the benefits of upgrading from Visual Studio 2005 to 2008? Any thoughts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.