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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:06:29+00:00 2026-05-26T15:06:29+00:00

When calling each on a hash in ruby, you can get the key and

  • 0

When calling each on a hash in ruby, you can get the key and value nicely separated like this:

{ :a => 1, :b => 2, :c => 3 }.each do |key, value| 
  puts "key is #{key} and value is #{value}"
end

=========================

key is :a and value is 1
key is :b and value is 2
key is :c and value is 3
=> {:a=>1, :b=>2, :c=>3}

However this doesn’t seem to work when using inject.

{ :a => 1, :b => 2, :c => 3 }.inject(0) do |result, key, value| 
  puts "key is #{key} and value is #{value}"
  result + value
end

=========================

key is [:a, 1] and value is
TypeError: nil can't be coerced into Fixnum

In the simplified example above I don’t really need the keys so I could just call hash.values.inject, but assuming I need both, is there a cleaner way to do this than this horrible bodge?

{ :a => 1, :b => 2, :c => 3 }.inject(0) do |result, key_and_value| 
  puts "key is #{key_and_value[0]} and value is #{key_and_value[1]}"
  result + key_and_value[1]
end
  • 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-26T15:06:30+00:00Added an answer on May 26, 2026 at 3:06 pm

    It looks like you need:

    { :a => 1, :b => 2, :c => 3 }.inject(0) do |result, (key, value)| 
        puts "key is #{key} and value is #{value}"
        result + value
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a method: def x(*a) Can calling it like so: x('foo', y: 'bar', z:
I am calling 5 external servers to retrieve XML-based data for each request for
When calling CoInitializeEx , you can specify the following values for dwCoInit : typedef
I have a hash object from an ActiveRecord. I'm going to be calling to_json
I get an error message from Firefox Unresponsive script. This error is due to
ASP.NET For each appSetting I use, I want to specify a value that will
I've Googled this question and can't seem to find a consistent opinion, or many
My webservice constructor is getting called each time that I call a webmethod. This
I have 2 activities. Activity A and Activity B. Both calling each other though
I'm trying to understand the following Ruby code. It looks like attrs is a

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.