# What is the uses of inject()

**URL:** <https://rubytalk.org/t/what-is-the-uses-of-inject/40740>\
**Category:** ruby-talk\
**Created:** [19 September 2007 10:06 UTC](https://rubytalk.org/t/what-is-the-uses-of-inject/40740 "2007-09-19T10:06:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arul\_hari](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@Arul\_hari](https://rubytalk.org/u/Arul_hari)\
**Post date:** [19 September 2007 10:06 UTC](https://rubytalk.org/t/what-is-the-uses-of-inject/40740/1 "2007-09-19T10:06:45Z")

</div>

Hello friends,  
Can anybody tell me the answer.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;what is the uses of inject().  
Give some example.

by  
Vellingiri.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [19 September 2007 10:53 UTC](https://rubytalk.org/t/what-is-the-uses-of-inject/40740/2 "2007-09-19T10:53:35Z")

</div>

Vellingiri Arul wrote:

> Hello friends,  
> Can anybody tell me the answer.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

> **···**
>
> --  
> NP: Porcupine Tree - Deadwing  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![anders1](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@anders1](https://rubytalk.org/u/anders1)\
**Post date:** [19 September 2007 10:55 UTC](https://rubytalk.org/t/what-is-the-uses-of-inject/40740/3 "2007-09-19T10:55:11Z")

</div>

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

> **···**
>
> On 19 Sep, 12:06, Vellingiri Arul \<hariharan....@rediffmail.com\> wrote:
> 
> > Hello friends,  
> > Can anybody tell me the answer.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;what is the uses of inject().  
> > Give some example.
> > 
> > by  
> > Vellingiri.  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [19 September 2007 10:59 UTC](https://rubytalk.org/t/what-is-the-uses-of-inject/40740/4 "2007-09-19T10:59:48Z")

</div>

> Hello friends,  
> Can anybody tell me the answer.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

> **···**
>
> 2007/9/19, Vellingiri Arul \<hariharan.spc@rediffmail.com\>:

---

<div class="post-metadata">

**Author:** ![Arul\_hari](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@Arul\_hari](https://rubytalk.org/u/Arul_hari)\
**Post date:** [19 September 2007 11:03 UTC](https://rubytalk.org/t/what-is-the-uses-of-inject/40740/5 "2007-09-19T11:03:04Z")

</div>

anders wrote:

> > Hello friends,  
> > Can anybody tell me the answer.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

> **···**
>
> > On 19 Sep, 12:06, Vellingiri Arul \<hariharan....@rediffmail.com\> \> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
