# Bloc param parens |memo, (a, b)|

**URL:** <https://www.ruby-forum.com/t/bloc-param-parens-memo-a-b/157939>\
**Category:** Ruby\
**Created:** [January 16, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/bloc-param-parens-memo-a-b/157939 "2009-01-16T19:02:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjholowaychuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@tjholowaychuk](https://www.ruby-forum.com/u/tjholowaychuk)\
**Post date:** [January 16, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/bloc-param-parens-memo-a-b/157939/1 "2009-01-16T19:02:40Z")

</div>

In Ruby \< 1.9 what does this really do? For example I wrote

class String  
def tokenize! hash  
hash.inject(self) { |s, (k, v)| s.gsub! /:#{k}/, v }  
end  
end

Which has a usage of:  
‘Welcome :name, enjoy your :object’.tokenize!({ :name =\> ‘TJ’, :object  
=\> ‘cookie’ })

anyways, the inject did not work as desired until I put parens around  
the last two parameters, yet I do not entirely understand whats going  
here! does this just cause the distribution of the variables to change?

---

<div class="post-metadata">

**Author:** ![tjholowaychuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@tjholowaychuk](https://www.ruby-forum.com/u/tjholowaychuk)\
**Post date:** [January 16, 2009, 7:29pm UTC](https://www.ruby-forum.com/t/bloc-param-parens-memo-a-b/157939/2 "2009-01-16T19:29:43Z")

</div>

On Jan 16, 2009, at 1:01 PM, Tj Holowaychuk wrote:

> =\> ‘cookie’ })
> 
> anyways, the inject did not work as desired until I put parens around  
> the last two parameters, yet I do not entirely understand whats going  
> here! does this just cause the distribution of the variables to  
> change?

When you enumerate over a hash you get a series of arrays. Each array  
has two elements: [key, value]. If your inject block only has two  
arguments  
defined, the second argument will be an array of two elements. When you  
insert the parens the second argument is decomposed according to Ruby’s  
multiple assignment rules. Like this:

k,v = array[0], array[1]

Gary W.

---

<div class="post-metadata">

**Author:** ![tjholowaychuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@tjholowaychuk](https://www.ruby-forum.com/u/tjholowaychuk)\
**Post date:** [January 16, 2009, 9:09pm UTC](https://www.ruby-forum.com/t/bloc-param-parens-memo-a-b/157939/3 "2009-01-16T21:09:19Z")

</div>

Thanks for the reply! I get it now, just never really took the time to  
check that stuff out

```
def test &block
  yield 1, [2, 3]
end

test do |a, b, c|
  p a
  p b
  p c
end

puts

test do |a, (b, c)|
  p a
  p b
  p c
end

```

that demonstrated it pretty well
