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

  • Home
  • SEARCH
  • 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 9242211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:32:10+00:00 2026-06-18T08:32:10+00:00

The documentation says: enum#each_with_object :- Iterates the given block for each element with an

  • 0

The documentation says:

enum#each_with_object
:-

Iterates the given block for each element with an arbitrary object, obj, and returns obj

enum#with_object:-

Iterates the given block for each element with an arbitrary object, obj, and returns obj

But when I tried the below on the both constructs, one gave me the output as expected but the others didn’t. So I suspect there is a difference between those two constructs.

Using each_with_object

%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
=> {"foo"=>"FOO", "bar"=>"BAR"}

success here!

Using with_object

%w(foo bar).with_object({}) { |str, hsh| hsh[str] = str.upcase }
 => NoMethodError: undefined method `with_object' for ["foo", "bar"]:Array
    from (irb):1
    from C:/Ruby193/bin/irb:12:in `<main>'

failed here!

So what is the difference between these two methods?

  • 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-06-18T08:32:11+00:00Added an answer on June 18, 2026 at 8:32 am

    each returns an Enumerator object.

    %w(foo bar).each.class
    => Enumerator
    

    So, for the first case, the array’ll be converted to Enumerator first, then works on the with_object.

    If you want the second case works, you have to convert the array to Enumerator. You can use .to_enum, .each, or .map to convert the array.

    %w(foo bar).map.with_object({}) { |str, hsh| hsh[str] = str.upcase }
    => {"foo"=>"FOO", "bar"=>"BAR"}
    

    More details: Enumerator

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

Sidebar

Related Questions

The documentation says that execute() must be called from a UI thread. But, since
The documentation says to use: $(input[type='checkbox']).bind( change, function(event, ui) { But I want to
The documentation says that camel-spring-ws(v2.7.1) officially supports spring-ws 1.5.9, but doesn't mention spring-ws 2.0.2.RELEASE.
Ravendb documentation says to simply set the DataDirectory name before initializing the DocumentStore, but
The documentation says that the ValidatorFactory is thread-safe, but I have a concern about
IronPython.net documentation says the MSIL in the assembly isn't CLS-compliant, but is there a
Documentation says it is a standalone component. Given I have following yaml parsed in
The documentation says about timerWithTimeInterval:target:selector:userInfo:repeats: Returns a new NSTimer that, when added to a
Documentation says that waypoints limit is 8 points. But I have to draw a
SimpleIni Documentation says wchar_t is supported but I don't understand how to use it.

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.