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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:09:54+00:00 2026-06-18T03:09:54+00:00

I recently ran into this issue: I’ve always used inject like so (i knew

  • 0

I recently ran into this issue:
I’ve always used inject like so (i knew that (0) part is optional and can be omitted)

array = [13,23,13]
#=> [13, 23, 13]
array.inject(0) { |sum,i| sum+i }
#=> 49

By chance i found out that you can use:

array.inject(:+)
#=> 49
array.inject(:-)
#=> -23
array.inject(:*)
#=> 3887
array.inject(:/)
#=> 0

Googling on the issue i found a nice article on inject, but no mentioning there about what i’ve tried….
Can anyone explain to me or give some info about these inject commands that I’ve just used?

  • 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-18T03:09:56+00:00Added an answer on June 18, 2026 at 3:09 am

    From the doc on Enumerable#inject:

    … If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.

    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, if you specify a symbol, it treats it as a method name and invokes this method on every element of the enumerable, replacing memo as stated above. Now, the math operators (+-*/) are just methods, nothing else. These lines produce identical result:

    13 + 23 # => 36
    13.+(23) # => 36
    13.send(:+, 23) # => 36
    

    When you pass a symbol to inject or reduce it uses the third form to dynamically apply that operator to elements:

    [1,2,3].inject(:+) # => 6
    

    This shorthand can be used with methods other than operators as well:

    [{"a"=>1}, {"b"=>2}].inject(:merge) # => {"a"=>1, "b"=>2} 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently ran into an issue where I used a property that was introduced
I recently ran into an issue on my development box. I can run my
I recently ran into an issue that could easily be solved using modulus division,
I have recently ran into this strange issue, I was trying to reference parent
I recently ran into a piece of code very much like this one: var
I recently ran into this issue when cross-testing a design in IE9 and IE10.
Ok I ran into this recently and I can't quite figure out why its
I recently ran into an issue where a query was causing a full table
A problem I recently ran into was that when trying to update a field
Recently I ran into a very strange issue: touching the screen of the iPhone

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.