Array.each

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

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]

···

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

Reading the documentation for Array methods each and each_index at
RDoc Documentation 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

0x083078D5.asc (3.65 KB)

···

On 2010/03/16 16:02 +0200, Phillip Curry 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 = array.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

--
--
Sven Agnew
CTO Kangohammer
web: www.kangohammer.com
tz : GMT +2

Decaffeinated coffee? Just Say No.
--

Phillip Curry 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 = array.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

···

--
Posted via http://www.ruby-forum.com/\.

that looks just about right

···

On Tue, Mar 16, 2010 at 10:07 PM, Gary Wright <gwtmp01@mac.com> wrote:

On Mar 16, 2010, at 10:02 AM, Phillip Curry 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]
>>

Gary Wright wrote:

···

On Mar 16, 2010, at 10:02 AM, Phillip Curry 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)
--
Posted via http://www.ruby-forum.com/\.

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 Wright

···

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

Gary Wright wrote:

On Mar 16, 2010, at 10:02 AM, Phillip Curry 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)