# Hash#inject argument syntax

**URL:** <https://www.ruby-forum.com/t/hash-inject-argument-syntax/66880>\
**Category:** Ruby\
**Created:** [August 30, 2006, 1:29am UTC](https://www.ruby-forum.com/t/hash-inject-argument-syntax/66880 "2006-08-30T01:29:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [August 30, 2006, 1:29am UTC](https://www.ruby-forum.com/t/hash-inject-argument-syntax/66880/1 "2006-08-30T01:29:04Z")

</div>

The code below works but I do not understand the “(key, value)”  
parenthesis syntax. I have not seen it before. Could someone  
explain?

–Brian

(from Rails’ active\_support)  
class Hash  
def symbolize\_keys  
inject({}) do |options, (key, value)|  
options[key.to\_sym] = value  
options  
end  
end  
end  
(above code from Rails’ active\_support)

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [August 30, 2006, 2:22am UTC](https://www.ruby-forum.com/t/hash-inject-argument-syntax/66880/2 "2006-08-30T02:22:56Z")

</div>

Brian B. wrote:

> The code below works but I do not understand the “(key, value)”  
> parenthesis syntax. I have not seen it before. Could someone  
> explain?
> 
> –Brian
> 
> (from Rails’ active\_support)  
> class Hash  
> def symbolize\_keys  
> inject({}) do |options, (key, value)|

Hash#each, as you may know, actually produces an Array  
of Arrays (key-value pairs). Here what is being passed  
is actually one of those pairs–the parenthesised syntax  
uses Ruby’s assignment rules and splats the Array to  
two distinct variables. Consider it the same as:

key, value = [‘key’, ‘value’]

> ```
> options[key.to_sym] = value
> options
> 
> ```
> 
> end  
> end  
> end  
> (above code from Rails’ active\_support)

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [August 30, 2006, 10:56am UTC](https://www.ruby-forum.com/t/hash-inject-argument-syntax/66880/3 "2006-08-30T10:56:29Z")

</div>

On 30.08.2006 02:23, Eero S. wrote:

> > inject({}) do |options, (key, value)|
> 
> Hash#each, as you may know, actually produces an Array  
> of Arrays (key-value pairs). Here what is being passed  
> is actually one of those pairs–the parenthesised syntax  
> uses Ruby’s assignment rules and splats the Array to  
> two distinct variables. Consider it the same as:
> 
> key, value = [‘key’, ‘value’]

The mechanism at work here is a more general pattern matching which also  
is done for regular assignments:

> > (a,(b,c),d,(e,(f,g))) = 1,[2,3],4,[5,[6,7]]  
> > =\> [1, [2, 3], 4, [5, [6, 7]]]
> 
> > a  
> > =\> 1
> 
> > b  
> > =\> 2
> 
> > c  
> > =\> 3
> 
> > d  
> > =\> 4
> 
> > e  
> > =\> 5
> 
> > f  
> > =\> 6
> 
> > g  
> > =\> 7

Works also with the star operator:

> > (a,(b,c),d,(e,(f,\*g))) = 1,[2,3],4,[5,[6,7,8]]  
> > =\> [1, [2, 3], 4, [5, [6, 7, 8]]]
> 
> > g  
> > =\> [7, 8]

Kind regards

```
robert
```
