Principle of le,err i mean biggest surprise!!??

Discussion in 'Ruby' started by globalrev, May 9, 2008.

  1. globalrev

    globalrev Guest

    puts "Input sentence:"
    sentence = gets.split()
    puts

    temp = sentence
    phrase = sentence
    puts ":", temp, sentence, phrase

    phrase[0] = temp[1]
    puts "oink", phrase, temp

    when changing phrase[0] this will change temp as well and even
    sentence! makes no sense and very surprising. how do i do what i want
    to do?

    so basically ami messing with pointers here or what?
    globalrev, May 9, 2008
    #1
    1. Advertising

  2. globalrev

    Matthew Moss Guest

    On May 8, 7:15 pm, globalrev <> wrote:
    > puts "Input sentence:"
    > sentence = gets.split()
    > puts
    >
    > temp = sentence
    > phrase = sentence
    > puts ":", temp, sentence, phrase
    >
    > phrase[0] = temp[1]
    > puts "oink", phrase, temp
    >
    > when changing phrase[0] this will change temp as well and even
    > sentence! makes no sense and very surprising. how do i do what i want
    > to do?
    >
    > so basically ami messing with pointers here or what?


    Essentially, yes, though I believe they are more commonly called
    references (and not in that C++ sort of way).

    When you do the assignment, phrase[0] = temp[1], you are simply making
    the 0th item of the phrase array refer to the same object that the 1st
    item of temp refers.

    You need to throw in a `dup` call (duplicate) where appropriate. If
    you want temp and phrase to be distinct from sentence, then change
    those assignments to:

    temp = sentence.dup
    phrase = sentence.dup

    If you wanted only phrase[0] to be distinct from temp[1], then:

    phrase[0] = temp[1].dup
    Matthew Moss, May 9, 2008
    #2
    1. Advertising

  3. On Thu, May 8, 2008 at 8:15 PM, globalrev <> wrote:
    > puts "Input sentence:"
    > sentence = gets.split()
    > puts
    >
    > temp = sentence
    > phrase = sentence
    > puts ":", temp, sentence, phrase
    >
    > phrase[0] = temp[1]
    > puts "oink", phrase, temp
    >
    > when changing phrase[0] this will change temp as well and even
    > sentence! makes no sense and very surprising. how do i do what i want
    > to do?
    >
    > so basically ami messing with pointers here or what?


    http://talklikeaduck.denhaven2.com/articles/2006/09/13/on-variables-values-and-objects
    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
    Rick DeNatale, May 9, 2008
    #3
  4. globalrev

    Ron Fox Guest

    globalrev wrote:
    > puts "Input sentence:"
    > sentence = gets.split()
    > puts
    >
    > temp = sentence
    > phrase = sentence
    > puts ":", temp, sentence, phrase
    >
    > phrase[0] = temp[1]
    > puts "oink", phrase, temp
    >
    > when changing phrase[0] this will change temp as well and even
    > sentence! makes no sense and very surprising.

    Well actually it makes perfect sense.. given what a Ruby variable
    actually is. See below.

    how do i do what i want
    > to do?
    >
    > so basically ami messing with pointers here or what?


    Yes you are. Variables in ruby are references to their underlying
    objects. Assignment assigns the references. Use the dup method to
    create copies.

    Ron.
    Ron Fox, May 9, 2008
    #4
  5. globalrev

    Marc Heiler Guest

    Marc Heiler, May 10, 2008
    #5
    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. Dave Howell
    Replies:
    20
    Views:
    246
    William James
    Feb 5, 2006
  2. Einar Boson
    Replies:
    6
    Views:
    182
    Rick DeNatale
    Feb 2, 2009
  3. matt neuburg
    Replies:
    1
    Views:
    96
    Rick DeNatale
    Feb 17, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    420
    Janos Sebok
    Jun 9, 2009
  5. Replies:
    1
    Views:
    556
Loading...

Share This Page