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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:35:02+00:00 2026-06-07T14:35:02+00:00

Here is a nested hash. We want to add all the value of subtotal

  • 0

Here is a nested hash. We want to add all the value of “subtotal” together in ruby’s way. Please be noted that the key of “0” and “1342119042142” could be any other unknown strings (number of keys is at least one. Could be more than one) when performing the addition.

{"0"=>{"lease_item_id"=>"3",
 "subtotal"=>"100"},
 "1342119042142"=>{"lease_item_id"=>"1",
 "subtotal"=>"100",
 "_destroy"=>"false"}}}

Thanks.

  • 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-07T14:35:08+00:00Added an answer on June 7, 2026 at 2:35 pm

    Like this:

    set up hash:

    s = {"0"=>{"lease_item_id"=>"3", "subtotal"=>"100"},
         "1342119042142"=>{"lease_item_id"=>"1", "subtotal"=>"100","_destroy"=>"false"}}
    

    calculate total:

    total = s.inject(0) { |i, j| i + j.last["subtotal"].to_i}
    

    Explanation: Look here for documentation. Basically inject is given an initial value (in the above code it is 0) and it passes the given value to the given block, where it gets set to what is returned from the block in each iteration. So in the above code, initially it is 0, on the first iteration it is set to 0 + 100 and now is equal to 100, and on the second [and final] iteration it is set to 100 + 100, 200.

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

Sidebar

Related Questions

I'm creating a nested model in Rails, but I want to add fields to
When trying to deal with a nested hash in ruby, I'm getting this error:
Suppose we have a nested hash here. a = {:0 => {:CA => {:count
I'm trying to update a hash that's nested in a MongDB document. I know
What I'm talking about here are nested classes. Essentially, I have two classes that
I'm trying to generate the attr_reader from a hash (with nested hash) so that
So here is a nested list [[1, 2], [3, 4]] I want to wrap
I have read quite a few posts here for matching nested braces in Ruby
Trying to create a nested hash in Perl that is populated with entries from
I am using a two LINQ queries(nested) Here's what I want my query to

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.