# Troubles with blocks

**URL:** <https://www.ruby-forum.com/t/troubles-with-blocks/124954>\
**Category:** Ruby\
**Created:** [January 4, 2008, 12:49am UTC](https://www.ruby-forum.com/t/troubles-with-blocks/124954 "2008-01-04T00:49:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbholden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rbholden](https://www.ruby-forum.com/u/rbholden)\
**Post date:** [January 4, 2008, 12:49am UTC](https://www.ruby-forum.com/t/troubles-with-blocks/124954/1 "2008-01-04T00:49:58Z")

</div>

Hi,

I am a Python programmer and I’ve just started learning Ruby. Until now,  
I’ve enjoyed it, but I am having troubles with some methods that require  
blocks.

This is a simple example that (I expect) summarizes my problems - how to  
obtain a list of pairs [index, value] (or [value, index], it doesn’t  
matter) from an array:

Python:

list(enumerate([‘a’,‘b’,‘c’]))  
=\> [(0, ‘a’), (1, ‘b’), (2, ‘c’)]

Ruby:

t = []; [‘a’,‘b’,‘c’].each\_with\_index { |\*x| t \<\< x }; t  
=\> [[“a”, 0], [“b”, 1], [“c”, 2]]

Which is a very ugly solution. How could I do this on Ruby without a  
temporal variable?

thanks

---

<div class="post-metadata">

**Author:** ![rbholden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rbholden](https://www.ruby-forum.com/u/rbholden)\
**Post date:** [January 4, 2008, 1:07am UTC](https://www.ruby-forum.com/t/troubles-with-blocks/124954/2 "2008-01-04T01:07:36Z")

</div>

On Jan 3, 2008 6:49 PM, Holden H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Python:  
> temporal variable?
> 
> ## thanks
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hmm, I can’t find a shorter way than what you’ve got. Why are you trying  
to  
get this kind of information? Maybe there’s something earlier in your  
code  
that can be made more Ruby-esque to make this easier for you.

Jason

---

<div class="post-metadata">

**Author:** ![rbholden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rbholden](https://www.ruby-forum.com/u/rbholden)\
**Post date:** [January 4, 2008, 1:09am UTC](https://www.ruby-forum.com/t/troubles-with-blocks/124954/3 "2008-01-04T01:09:56Z")

</div>

On Jan 3, 2008, at 6:49 PM, Holden H. wrote:

> Ruby:
> 
> t = []; [‘a’,‘b’,‘c’].each\_with\_index { |\*x| t \<\< x }; t  
> =\> [[“a”, 0], [“b”, 1], [“c”, 2]]
> 
> Which is a very ugly solution. How could I do this on Ruby without a  
> temporal variable?

In Ruby 1.8:

require ‘enumerator’  
%w{a b c}.enum\_for(:each\_with\_index).to\_a

In Ruby 1.9:

%w{a b c}.each\_with\_index.to\_a

Gary W.

---

<div class="post-metadata">

**Author:** ![rbholden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rbholden](https://www.ruby-forum.com/u/rbholden)\
**Post date:** [January 4, 2008, 1:14am UTC](https://www.ruby-forum.com/t/troubles-with-blocks/124954/4 "2008-01-04T01:14:37Z")

</div>

On Fri, 4 Jan 2008 08:49:54 +0900, Holden H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Which is a very ugly solution. How could I do this on Ruby without a  
> temporal variable?

Try this (you will need to require ‘enumerator’):

[‘a’,‘b’,‘c’].enum\_with\_index.to\_a

-mental

---

<div class="post-metadata">

**Author:** ![rbholden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rbholden](https://www.ruby-forum.com/u/rbholden)\
**Post date:** [January 4, 2008, 1:18am UTC](https://www.ruby-forum.com/t/troubles-with-blocks/124954/5 "2008-01-04T01:18:08Z")

</div>

On Jan 3, 2008 7:13 PM, MenTaLguY [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > =\> [[“a”, 0], [“b”, 1], [“c”, 2]]

Ah, that’s where enum\_with\_index is. I was wondering why it wasn’t  
working  
for me.

Jason

---

<div class="post-metadata">

**Author:** ![rbholden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rbholden](https://www.ruby-forum.com/u/rbholden)\
**Post date:** [January 4, 2008, 1:18am UTC](https://www.ruby-forum.com/t/troubles-with-blocks/124954/6 "2008-01-04T01:18:04Z")

</div>

Gary W. wrote:

> In Ruby 1.8:
> 
> require ‘enumerator’  
> %w{a b c}.enum\_for(:each\_with\_index).to\_a
> 
> In Ruby 1.9:
> 
> %w{a b c}.each\_with\_index.to\_a

Thanks! that’s exactly what I was looking for. I had already tried the  
last solution but I got a “no block given” error (I’m using Ruby 1.8).  
I’m glad to hear that versions 1.9 and higher support this construct.
