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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:58:59+00:00 2026-05-14T00:58:59+00:00

I am creating a rails app and have used this code in one of

  • 0

I am creating a rails app and have used this code in one of my methods

item_numbers.inject(0) {|sum, i| sum + i.amount}

item_numbers is an array of objects from my item_numbers table. The .amount method that I apply to them looks up the value of an item_number in a separate table and returns it as a BigDecimal object. Obviously the inject method then adds all of the returned i.amount objects and this works just fine.

I am just curious as to why it didn’t work when I wrote this statement as

item_numbers.inject {|sum, i| sum + i.amount}

According to my trusty pickaxe book these should be equivalent. Is it because i.amount is a BigDecimal? If so, why does it now work? If not, then why doesn’t it work.

  • 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-14T00:58:59+00:00Added an answer on May 14, 2026 at 12:58 am

    What we can read in API:

    If you do not explicitly specify an
    initial value for memo, then uses the
    first element of collection is used as
    the initial value of memo.

    So item_numbers[0] will be specified as an initial value – but it is not a number, it is an object. So we have got an error

    undefined method `+’.

    So we have to specify initial value as 0

    item_numbers.inject(0){ |sum, i| sum + i }

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

Sidebar

Related Questions

I am creating a Rails app (fairly new at this), and I have a
This is my first rails app, one I'm creating for the sole purpose of
I have a JavaScript widget which communicates with my Rails app by creating tags
I am creating a rails app. And i have login in such a way
I am creating a new rails app as a learning project. I have already
I have some problem with creating a function to my Rails app. I want
I am creating a Rails App (I'd like to know how to do this
Consider this scenario. We have an internal Rails 2 app that connects to a
I'm creating a Rails app, and I have a model called User . In
I've used the following code in rails 2.3 without having to have a named

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.