How come this doesn't work as expected?

C

Chris Gehlker

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
 
Z

Zach Dennis

Chris said:
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

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
 
C

Chris Gehlker

String#to_i doesn't mutate the string.

Chris said:
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

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

---
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
 
J

James Edward Gray II

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
 
J

James G. Britt

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
 
B

benny

Chris said:
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)
 
C

Chris Gehlker

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top