[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How come this doesn't work as expected?

Chris Gehlker

2/7/2005 3:09:00 PM

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)



7 Answers

Zach Dennis

2/7/2005 3:17:00 PM

0

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


Sam Goldman

2/7/2005 3:20:00 PM

0

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


Chris Gehlker

2/7/2005 4:01:00 PM

0


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.

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



James Gray

2/7/2005 4:12:00 PM

0

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



ruby talk

2/7/2005 4:18:00 PM

0

On Tue, 8 Feb 2005 01:01:19 +0900, Chris Gehlker <canyonrat@mac.com> wrote:
>
> 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


benny

2/7/2005 4:51:00 PM

0

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)

Chris Gehlker

2/7/2005 4:54:00 PM

0


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

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

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