# I don't really understand inject

**URL:** <https://www.ruby-forum.com/t/i-dont-really-understand-inject/140891>\
**Category:** Ruby\
**Created:** [June 19, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/i-dont-really-understand-inject/140891 "2008-06-19T15:15:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asplake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@asplake](https://www.ruby-forum.com/u/asplake)\
**Post date:** [June 19, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/i-dont-really-understand-inject/140891/1 "2008-06-19T15:15:59Z")

</div>

I think these methods should both do the same thing. The second works,  
however in the first I get errors that string is not matched, where  
hash is an instance of String not hash

def sequences  
self.alignment.split(/\n/).inject(Hash.new) do |hash, line|  
id, sequence = line.split(/\s+/,2)  
# hash.class == String  
hash[id] = sequence  
end  
end

def sequences  
hash = Hash.new  
self.alignment.split(/\n/).each do |line|  
id, sequence = line.split(/\s+/,2)  
# hash.class == Hash  
hash[id] = sequence  
end  
hash  
end

---

<div class="post-metadata">

**Author:** ![asplake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@asplake](https://www.ruby-forum.com/u/asplake)\
**Post date:** [June 19, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/i-dont-really-understand-inject/140891/2 "2008-06-19T15:34:56Z")

</div>

On Jun 19, 2008, at 9:13 AM, Mike B. wrote:

> I think these methods should both do the same thing. The second works,  
> however in the first I get errors that string is not matched, where  
> hash is an instance of String not hash
> 
> def sequences  
> self.alignment.split(/\n/).inject(Hash.new) do |hash, line|  
> id, sequence = line.split(/\s+/,2)  
> # hash.class == String  
> hash[id] = sequence  
> hash # value of block will be the last expression  
> end  
> hash  
> end

The value of the block becomes the first block parameter in the next  
iteration.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![asplake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@asplake](https://www.ruby-forum.com/u/asplake)\
**Post date:** [June 19, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/i-dont-really-understand-inject/140891/3 "2008-06-19T15:37:54Z")

</div>

On Thu, Jun 19, 2008 at 3:13 PM, Mike B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> How inject works is that it passes the block two values:  
> an accumulator and one of the elements in the enumerable  
> (one at a time). In each iteration, the accumulator will be the  
> return value of the block in the previous iteration.

So in your example, if you want the accumulator to be a hash,  
your block should return a hash. Try this (untested):

def sequences  
self.alignment.split(/\n/).inject(Hash.new) do |hash, line|  
id, sequence = line.split(/\s+/,2)  
# hash.class == String  
hash[id] = sequence  
hash # \<\<\< the change  
end  
end

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![asplake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@asplake](https://www.ruby-forum.com/u/asplake)\
**Post date:** [June 19, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/i-dont-really-understand-inject/140891/4 "2008-06-19T16:14:51Z")

</div>

I see, I see, I see.  
That solved the problem. Thanks for that help injection!  
Ha Ha Ha!

On Jun 19, 2:36 pm, Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![asplake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@asplake](https://www.ruby-forum.com/u/asplake)\
**Post date:** [June 19, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/i-dont-really-understand-inject/140891/5 "2008-06-19T16:24:39Z")

</div>

On Thu, Jun 19, 2008 at 4:08 PM, Mike B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I see, I see, I see.  
> That solved the problem. Thanks for that help injection!  
> Ha Ha Ha!  
> Nice one, fortunately you did not ask for help on reduce (which is an  
> alias of inject) ;).  
> Robert
