Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How come this doesn't work as expected?

Reply
Thread Tools

How come this doesn't work as expected?

 
 
Chris Gehlker
Guest
Posts: n/a
 
      02-07-2005
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)



 
Reply With Quote
 
 
 
 
Zach Dennis
Guest
Posts: n/a
 
      02-07-2005
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


 
Reply With Quote
 
 
 
 
Sam Goldman
Guest
Posts: n/a
 
      02-07-2005
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)
>
>



 
Reply With Quote
 
Chris Gehlker
Guest
Posts: n/a
 
      02-07-2005

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



 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      02-07-2005
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



 
Reply With Quote
 
James G. Britt
Guest
Posts: n/a
 
      02-07-2005
On Tue, 8 Feb 2005 01:01:19 +0900, Chris Gehlker <(E-Mail Removed)> 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


 
Reply With Quote
 
benny
Guest
Posts: n/a
 
      02-07-2005
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)
 
Reply With Quote
 
Chris Gehlker
Guest
Posts: n/a
 
      02-07-2005

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)




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
please come see my work Albert Digital Photography 1 05-31-2005 12:19 AM
How come this C++ segment doesn't work? Sea Squid C++ 6 03-17-2005 08:43 PM
how come .insert() don't work Bennie Python 9 10-28-2004 10:05 AM
Come One, Come All Jess Guim Digital Photography 0 12-02-2003 04:43 PM



Advertisments