# Array#inject with hash as initial, unexpected error

**URL:** <https://rubytalk.org/t/array-inject-with-hash-as-initial-unexpected-error/25702>\
**Category:** ruby-talk\
**Created:** [13 March 2006 01:45 UTC](https://rubytalk.org/t/array-inject-with-hash-as-initial-unexpected-error/25702 "2006-03-13T01:45:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_D\_Moss](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@Matthew\_D\_Moss](https://rubytalk.org/u/Matthew_D_Moss)\
**Post date:** [13 March 2006 01:45 UTC](https://rubytalk.org/t/array-inject-with-hash-as-initial-unexpected-error/25702/1 "2006-03-13T01:45:20Z")

</div>

(On Mac OS X 10.4.5, Ruby 1.8.4)

Okay, so:

> [1, 2, 3].inject(0) { |s, v| s += v }

=\> 6

and:

> [1, 2, 3].inject() { |a, v| a \<\< v\*\*2 }

=\> [1, 4, 9]

but:

> [1, 2, 3].inject({}) { |h, v| h[v] = v\*\*2 }

=\> NoMethodError: undefined method `=' for 1:Fixnum

What gives?  
I've tried replacing {} with Hash.new and a couple other variants without luck.

---

<div class="post-metadata">

**Author:** ![Trevor\_Squires](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Trevor\_Squires](https://rubytalk.org/u/Trevor_Squires)\
**Post date:** [13 March 2006 01:59 UTC](https://rubytalk.org/t/array-inject-with-hash-as-initial-unexpected-error/25702/2 "2006-03-13T01:59:03Z")

</div>

Hey Matthew,

observe:

irb(main):001:0\> \<\< 100  
=\> [100]  
irb(main):002:0\> {}[:something] = 100  
=\> 100

The \<\< operator on an array returns the array, while the operator on a hash returns the value you assigned.

And the result of the last statement in your inject block will replace the value for your memo on each iteration.

So... just make sure the last statement in your inject block is the memo value you are accumulating:

[1,2,3].inject({}) { |memo, val| memo[val] = val\*\*2; memo}

HTH,  
Trevor

> **···**
>
> --  
> Trevor Squires  
> [http://somethinglearned.com](http://somethinglearned.com)
> 
> On 12-Mar-06, at 5:45 PM, Matthew Moss wrote:
> 
> > (On Mac OS X 10.4.5, Ruby 1.8.4)
> > 
> > Okay, so:
> > 
> > > [1, 2, 3].inject(0) { |s, v| s += v }
> > 
> > =\> 6
> > 
> > and:
> > 
> > > [1, 2, 3].inject() { |a, v| a \<\< v\*\*2 }
> > 
> > =\> [1, 4, 9]
> > 
> > but:
> > 
> > > [1, 2, 3].inject({}) { |h, v| h[v] = v\*\*2 }
> > 
> > =\> NoMethodError: undefined method `=' for 1:Fixnum
> > 
> > What gives?  
> > I've tried replacing {} with Hash.new and a couple other variants without luck.

---

<div class="post-metadata">

**Author:** ![Matthew\_D\_Moss](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@Matthew\_D\_Moss](https://rubytalk.org/u/Matthew_D_Moss)\
**Post date:** [13 March 2006 03:59 UTC](https://rubytalk.org/t/array-inject-with-hash-as-initial-unexpected-error/25702/3 "2006-03-13T03:59:02Z")

</div>

Okay, I feel silly now. Thanks for the reminder on basic assignment  
protocol. =)

> **···**
>
> On 3/12/06, Trevor Squires \<trevor@protocool.com\> wrote:
> 
> > Hey Matthew,
> > 
> > observe:
> > 
> > irb(main):001:0\> \<\< 100  
> > =\> [100]  
> > irb(main):002:0\> {}[:something] = 100  
> > =\> 100
> > 
> > The \<\< operator on an array returns the array, while the operator  
> > on a hash returns the value you assigned.
> > 
> > And the result of the last statement in your inject block will  
> > replace the value for your memo on each iteration.
> > 
> > So... just make sure the last statement in your inject block is the  
> > memo value you are accumulating:
> > 
> > [1,2,3].inject({}) { |memo, val| memo[val] = val\*\*2; memo}
