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 7938331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:46:57+00:00 2026-06-03T22:46:57+00:00

a = [[1, ‘a’],[2, ‘b’],[3, ‘c’], [4, ‘d’]] a.inject({}) {|r, val| r[val[0]] = val[1]}

  • 0
a = [[1, 'a'],[2, 'b'],[3, 'c'], [4, 'd']]
a.inject({}) {|r, val| r[val[0]] = val[1]}

When I run this, I get an index error

When I change the block to

a.inject({}) {|r, val| r[val[0]] = val[1]; r}

It then works.

How is ruby handling the first inject attempt that isn’t getting what I want?
Is there a better way to do this?

  • 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-03T22:46:59+00:00Added an answer on June 3, 2026 at 10:46 pm

    Just because Ruby is dynamically and implicitly typed doesn’t mean that you don’t have to think about types.

    The type of Enumerable#inject without an explicit accumulator (this is usually called reduce) is something like

    reduce :: [a] → (a → a → a) → a
    

    or in a more Rubyish notation I just made up

    Enumerable[A]#inject {|A, A| A } → A
    

    You will notice that all the types are the same. The element type of the Enumerable, the two argument types of the block, the return type of the block and the return type of the overall method.

    The type of Enumerable#inject with an explicit accumulator (this is usually called fold) is something like

    fold :: [b] → a → (a → b → a) → a
    

    or

    Enumerable[B]#inject(A) {|A, B| A } → A
    

    Here you see that the accumulator can have a different type than the element type of the collection.

    These two rules generally get you through all Enumerable#inject-related type problems:

    1. the type of the accumulator and the return type of the block must be the same
    2. when not passing an explicit accumulator, the type of the accumulator is the same as the element type

    In this case, it is Rule #1 that bites you. When you do something like

    acc[key] = value
    

    in your block, assignments evaluate to the assigned value, not the receiver of the assignment. You’ll have to replace this with

    acc.tap { acc[key] = value }
    

    See also Why Ruby inject method cannot sum up string lengths without initial value?


    BTW: you can use destructuring bind to make your code much more readable:

    a.inject({}) {|r, (key, value)| r[key] = value; r }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi if i run my project its indicate this logcat error...how to solve this
This is how I get the tags of a body of text. var tags
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I was writing code for dragging mechanism which invokes to wait for small period
I downloaded PostgreSQL from the official website and ran the .dmg installer. After that

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.