How come this doesn't work as expected?

Discussion in 'Ruby' started by Chris Gehlker, Feb 7, 2005.

  1. 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, Feb 7, 2005
    #1
    1. Advertising

  2. Chris Gehlker

    Zach Dennis Guest

    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
     
    Zach Dennis, Feb 7, 2005
    #2
    1. Advertising

  3. Chris Gehlker

    Sam Goldman Guest

    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 Goldman, Feb 7, 2005
    #3
  4. 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
     
    Chris Gehlker, Feb 7, 2005
    #4
  5. 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
     
    James Edward Gray II, Feb 7, 2005
    #5
  6. On Tue, 8 Feb 2005 01:01:19 +0900, 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.


    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
     
    James G. Britt, Feb 7, 2005
    #6
  7. Chris Gehlker

    benny Guest

    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)
     
    benny, Feb 7, 2005
    #7
  8. 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)
     
    Chris Gehlker, Feb 7, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith Patrick
    Replies:
    2
    Views:
    3,636
    Keith Patrick
    Nov 16, 2005
  2. Sea Squid
    Replies:
    6
    Views:
    2,330
    Heinz Ozwirk
    Mar 17, 2005
  3. michaaal
    Replies:
    5
    Views:
    156
    Bob Barrows [MVP]
    Jul 2, 2004
  4. Hey You

    How come this doesn't work?

    Hey You, Apr 11, 2007, in forum: Ruby
    Replies:
    13
    Views:
    162
    Eleanor McHugh
    Apr 11, 2007
  5. Jack Jones

    How come this doesn't work ?

    Jack Jones, Mar 16, 2007, in forum: Javascript
    Replies:
    8
    Views:
    110
    Walton
    Mar 16, 2007
Loading...

Share This Page