# Array.each

**URL:** <https://www.ruby-forum.com/t/array-each/185234>\
**Category:** Ruby\
**Created:** [March 16, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/array-each/185234 "2010-03-16T15:03:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcalcado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a698b9/32.png) [@pcalcado](https://www.ruby-forum.com/u/pcalcado)\
**Post date:** [March 16, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/array-each/185234/1 "2010-03-16T15:03:15Z")

</div>

I want to take an array of strings and convert each member to an  
integer.  
array.to\_i obviously doesn’t work.  
I tried array.each { |x| x = x.to\_i } and that doesn’t work either.  
aray.each\_index { |x| array[x] = array[x].to\_i } works fine. I’m  
finding  
that pretty much every time I try to use each, each\_index works better.  
Why  
doesn’t each work here, is there anything I can do about it, and is  
there  
anything in particular that each does better than each\_index or should I  
just drop it entirely?

thanks  
phil

---

<div class="post-metadata">

**Author:** ![pcalcado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a698b9/32.png) [@pcalcado](https://www.ruby-forum.com/u/pcalcado)\
**Post date:** [March 16, 2010, 3:07pm UTC](https://www.ruby-forum.com/t/array-each/185234/2 "2010-03-16T15:07:56Z")

</div>

On Mar 16, 2010, at 10:02 AM, Phillip C. wrote:

> I want to take an array of strings and convert each member to an integer.  
> array.to\_i obviously doesn’t work.

=\> a = %w{1 2 3 4}  
=\> [“1”, “2”, “3”, “4”]

> > a.map! { |x| x.to\_i }  
> > =\> [1, 2, 3, 4]

---

<div class="post-metadata">

**Author:** ![pcalcado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a698b9/32.png) [@pcalcado](https://www.ruby-forum.com/u/pcalcado)\
**Post date:** [March 16, 2010, 3:20pm UTC](https://www.ruby-forum.com/t/array-each/185234/3 "2010-03-16T15:20:13Z")

</div>

On 2010/03/16 16:02 +0200, Phillip C. wrote:

> phil

Reading the documentation for Array methods each and each\_index at  
[RDoc Documentation](http://ruby-doc.org/core/) I’m wondering if the results you’re seeing are  
exactly what you expect since the indices passed in each\_index are  
already integers

## Ciao, Sven

–  
Sven Agnew  
CTO Kangohammer  
web: [www.kangohammer.com](http://www.kangohammer.com)  
tz : GMT +2

Decaffeinated coffee? Just Say No.

---

<div class="post-metadata">

**Author:** ![pcalcado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a698b9/32.png) [@pcalcado](https://www.ruby-forum.com/u/pcalcado)\
**Post date:** [March 16, 2010, 3:27pm UTC](https://www.ruby-forum.com/t/array-each/185234/4 "2010-03-16T15:27:15Z")

</div>

that looks just about right

---

<div class="post-metadata">

**Author:** ![pcalcado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a698b9/32.png) [@pcalcado](https://www.ruby-forum.com/u/pcalcado)\
**Post date:** [March 17, 2010, 3:42am UTC](https://www.ruby-forum.com/t/array-each/185234/5 "2010-03-17T03:42:59Z")

</div>

Phillip C. wrote:

> I want to take an array of strings and convert each member to an  
> integer.  
> array.to\_i obviously doesn’t work.  
> I tried array.each { |x| x = x.to\_i } and that doesn’t work either.  
> aray.each\_index { |x| array[x] = array[x].to\_i } works fine. I’m  
> finding  
> that pretty much every time I try to use each, each\_index works better.  
> Why  
> doesn’t each work here, is there anything I can do about it, and is  
> there  
> anything in particular that each does better than each\_index or should I  
> just drop it entirely?
> 
> thanks  
> phil

The issue is one of scope of local variables. x is a block parameter,  
and is local to the block. That is why when you change the value of x  
within the block, it has no affect on array. Each time the block  
executes, the value of an element in array is copied to x, but changing  
x doesn’t change the value of array. The .each\_index method works  
because array was initialized outside of the block and is available  
within within the block itself (but x itself is still local to the  
block).

There are times when .each is appropriate, but for what you are doing,  
.each\_index is what you need.  
-Alex

---

<div class="post-metadata">

**Author:** ![pcalcado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a698b9/32.png) [@pcalcado](https://www.ruby-forum.com/u/pcalcado)\
**Post date:** [March 17, 2010, 6:27am UTC](https://www.ruby-forum.com/t/array-each/185234/6 "2010-03-17T06:27:25Z")

</div>

On Mar 17, 2010, at 1:15 AM, Sam Yang wrote:

> simply  
> [“1”,“2”].collect(&:to\_i)

simply?

Your alternative creates a new array and it seemed like the OP  
wanted to replace the elements in the existing array. And I don’t  
think Symbol#to\_proc is ‘simple’ for someone who isn’t familiar yet  
with map/map! (or collect/collect!).

Gary W.

---

<div class="post-metadata">

**Author:** ![pcalcado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a698b9/32.png) [@pcalcado](https://www.ruby-forum.com/u/pcalcado)\
**Post date:** [March 17, 2010, 6:15am UTC](https://www.ruby-forum.com/t/array-each/185234/7 "2010-03-17T06:15:33Z")

</div>

Gary W. wrote:

> On Mar 16, 2010, at 10:02 AM, Phillip C. wrote:
> 
> > I want to take an array of strings and convert each member to an integer.  
> > array.to\_i obviously doesn’t work.
> 
> =\> a = %w{1 2 3 4}  
> =\> [“1”, “2”, “3”, “4”]
> 
> > > a.map! { |x| x.to\_i }  
> > > =\> [1, 2, 3, 4]

simply  
[“1”,“2”].collect(&:to\_i)
