# What is the uses of inject()

**URL:** <https://www.ruby-forum.com/t/what-is-the-uses-of-inject/114971>\
**Category:** Ruby\
**Created:** [September 19, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/what-is-the-uses-of-inject/114971 "2007-09-19T12:06:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [September 19, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/what-is-the-uses-of-inject/114971/1 "2007-09-19T12:06:32Z")

</div>

Hello friends,  
Can anybody tell me the answer.

```
         what is the uses of inject().

```

Give some example.

by  
Vellingiri.

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [September 19, 2007, 12:54pm UTC](https://www.ruby-forum.com/t/what-is-the-uses-of-inject/114971/2 "2007-09-19T12:54:34Z")

</div>

Vellingiri A. wrote:

> Hello friends,  
> Can anybody tell me the answer.  
> what is the uses of inject().

ri Enumerable#inject can give you a more elaborate answer, but here’s  
mine:  
It allows you to combine the elements of an array using a block (passing  
the  
initial value or the result of the last iteration as well as the current  
item  
to the block each iteration).

> Give some example.

[1,2,3,4].inject {|s,i| s+i}  
#=\> 10 (1+2+3+4)

some\_string=“haolaolai foo tadam tadum bar HuHu chunky lamb bacon”  
regexen=[/la./,/foo.\*bar/,/huhu/i]  
regexen.inject(some\_string) {|str,re| str.gsub(re,"")}  
#=\> “hao chunky b bacon”  
#(same as  
some\_string.gsub(/la./,"").gsub(/foo.\*bar/,"").gsub(/huhu/i,"") )

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [September 19, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/what-is-the-uses-of-inject/114971/3 "2007-09-19T12:57:34Z")

</div>

On 19 Sep, 12:06, Vellingiri A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello friends,  
> Can anybody tell me the answer.
> 
> ```
> what is the uses of inject().
> 
> ```
> 
> Give some example.
> 
> ## by Vellingiri.
> 
> Posted viahttp://www.ruby-forum.com/.

You have a lot of Q and what examples have a look in  
[http://www.ruby-doc.org/docs/UsersGuide/rg/](http://www.ruby-doc.org/docs/UsersGuide/rg/)  
//AP

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [September 19, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/what-is-the-uses-of-inject/114971/4 "2007-09-19T13:03:03Z")

</div>

anders wrote:

> On 19 Sep, 12:06, Vellingiri A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hello friends,  
> > Can anybody tell me the answer.
> > 
> > ```
> > what is the uses of inject().
> > 
> > ```
> > 
> > Give some example.
> > 
> > ## by Vellingiri.
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> You have a lot of Q and what examples have a look in  
> [http://www.ruby-doc.org/docs/UsersGuide/rg/](http://www.ruby-doc.org/docs/UsersGuide/rg/)  
> //AP

very very thank you friends.

You have asked one question know,that is not a example program question.  
I am also working one software company,I have faced lot of doubts in my  
ruby program.  
Some thing general what we were discussed the questions.

Ok.

by

vellingiri.

---

<div class="post-metadata">

**Author:** ![vellingiri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5fc32e/32.png) [@vellingiri](https://www.ruby-forum.com/u/vellingiri)\
**Post date:** [September 19, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/what-is-the-uses-of-inject/114971/5 "2007-09-19T13:00:34Z")

</div>

2007/9/19, Vellingiri A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hello friends,  
> Can anybody tell me the answer.
> 
> ```
> what is the uses of inject().
> 
> ```

[http://errtheblog.com/post/633](http://errtheblog.com/post/633)

> Give some example.

[http://www.dtcc.edu/cs/rfc1855.html#3](http://www.dtcc.edu/cs/rfc1855.html#3)

A general hint: search engines can go a long way in helping answering  
questions.

Regards

robert
