How come this doesn't work as expected?

I'm just curious. I already found a work-around.

testAry = ["5", "7", "9"]
p testAry
testAry = testAry.each { |n| n.to_i}
p testAry

···

------------------------------
The second output is still an array of strings.

--
Conscience is thoroughly well-bred and soon leaves off talking to those who do not wish to hear it.
-Samuel Butler, writer (1835-1902)

Chris Gehlker wrote:

I'm just curious. I already found a work-around.

testAry = ["5", "7", "9"]
p testAry
testAry = testAry.each { |n| n.to_i}
p testAry
------------------------------
The second output is still an array of strings.

each doesn't modify the element in place. Use map or collect instead.

irb(main):001:0> t = [ "1", "2", "3" ]
=> ["1", "2", "3"]
irb(main):002:0> t.map{ |n| n.to_i }
=> [1, 2, 3]
irb(main):003:0>

Zach

String#to_i doesn't mutate the string.

Chris Gehlker wrote:

···

I'm just curious. I already found a work-around.

testAry = ["5", "7", "9"]
p testAry
testAry = testAry.each { |n| n.to_i}
p testAry
------------------------------
The second output is still an array of strings.

--
Conscience is thoroughly well-bred and soon leaves off talking to those who do not wish to hear it.
-Samuel Butler, writer (1835-1902)

Sam and Zach,

I get your points. My question was how come the assignment back to testAry doesn't replace it with an array of numbers. Here is a different version of the program which may highlight the problem.

testAry = ["5", "7", "9"]
p testAry
numAry = testAry.each { |n| n.to_i}
p numAry

···

On Feb 7, 2005, at 8:20 AM, Sam Goldman wrote:

String#to_i doesn't mutate the string.

Chris Gehlker wrote:

I'm just curious. I already found a work-around.
testAry = ["5", "7", "9"]
p testAry
testAry = testAry.each { |n| n.to_i}
p testAry
------------------------------
The second output is still an array of strings.

---
If you came and you found a strange man... teaching your kids to punch each other, or trying to sell them all kinds of products, you'd kick him right out of the house, but here you are; you come in and the TV is on, and you don't think twice about it.
-Jerome Singer

You miss understand what each() does. It does return the array, but this is rarely used for anything more than chaining. The block of each, does something with the members of the array, but it does not replace those members. That's what map/collect are for.

Hope that helps.

James Edward Gray II

···

On Feb 7, 2005, at 10:01 AM, Chris Gehlker wrote:

Sam and Zach,

I get your points. My question was how come the assignment back to testAry doesn't replace it with an array of numbers. Here is a different version of the program which may highlight the problem.

I get your points. My question was how come the assignment back to
testAry doesn't replace it with an array of numbers.

Because the return value of 'each' is the (unchanged) receiver, not
the accumulated results of the block operating on the array contents.
.

Here is a
different version of the program which may highlight the problem.

testAry = ["5", "7", "9"]
p testAry
numAry = testAry.each { |n| n.to_i}
p numAry

"testAry.each { |n| n.to_i} " returns testAry. The "each" call did
not alter testAry, so you see the orignal values.

James

···

On Tue, 8 Feb 2005 01:01:19 +0900, Chris Gehlker <canyonrat@mac.com> wrote:

Chris Gehlker wrote:

I get your points. My question was how come the assignment back to
testAry doesn't replace it with an array of numbers. Here is a
different version of the program which may highlight the problem.

testAry = ["5", "7", "9"]
p testAry
numAry = testAry.each { |n| n.to_i}
p numAry

testAry = ["5", "7", "9"]
p testAry
numAry = testAry.collect { |n| n.to_i}
p numAry

does what you expect. each only iterates over the array.

benny

···

--
---------------------------------------------------------------------------------------------------
Don't crash when a filter changes the subject of a message which results
in the attempt to remove it from the tree of subject threading messages
failing and the detached child looking for a new parent finding the old
parent as the new parent, which in turn results in the child being deleted
with the old (and new) parent, which is not a good idea, since it is
still referenced.

(Till Adams commit on kdepim/kmail/kmheaders.cpp in HEAD, 6. Jan. 2005)

Yes, you and Sam and Zach finally helped me see the light. Thanks so much.

···

On Feb 7, 2005, at 9:12 AM, James Edward Gray II wrote:

On Feb 7, 2005, at 10:01 AM, Chris Gehlker wrote:

Sam and Zach,

I get your points. My question was how come the assignment back to testAry doesn't replace it with an array of numbers. Here is a different version of the program which may highlight the problem.

You miss understand what each() does. It does return the array, but this is rarely used for anything more than chaining. The block of each, does something with the members of the array, but it does not replace those members. That's what map/collect are for.

Hope that helps.

James Edward Gray II

--
Conscience is thoroughly well-bred and soon leaves off talking to those who do not wish to hear it.
-Samuel Butler, writer (1835-1902)