Professional Web Applications Themes

I don't understand this behavour, is it a bug? - Ruby

Please I wonder if any one can give me some advice? I thought I would get into Ruby by "writing BASIC scripts in Ruby". I thought I would try to write one script fragment each day. My latest script fragment has a behavour which I do not understand. Is it a bug? I give variable one a value of [1, 2, 3, 4, 5] . I then set variables two and three equal to variable one. I then do stuff to three using one to give three the value [1, 3, 6, 10, 15]. My little scrip then prints out ...

  1. #1

    Default I don't understand this behavour, is it a bug?

    Please I wonder if any one can give me some advice?

    I thought I would get into Ruby by "writing BASIC scripts in Ruby". I
    thought I would try to write one script fragment each day.
    My latest script fragment has a behavour which I do not understand. Is it a
    bug?

    I give variable one a value of [1, 2, 3, 4, 5] . I then set variables two
    and three equal to variable one. I then do stuff to three using one to give
    three the value [1, 3, 6, 10, 15]. My little scrip then prints out variables
    one, two and three. I am amazed to find that one and two appear in the
    printout to have altered them selves to look like three, that is one == two
    == three == [1, 3, 6, 10, 15].
    WHY?? How on earth can one and two have altered themselves.

    According to the help menu version screen I used RubyWin 0.0.38 Ruby 1.6.5
    Scintilla 1.38

    This is my little ruby scrip file that I ran on Rubywin: PLEASE NOT I HAVE A
    KOREAN COMPUTER WHICH SHOWS THE BACK SLASH AS THE KOREAN MONEY SYMBOL \ =
    WON

    puts " Get length of number series: "

    upto = (gets.strip).to_i
    puts "\n"

    one = (1..upto).to_a
    two = one
    three = one

    three[0] = one[0]
    for i in 1..(one.length - 1)
    three[i] = one[i] + three[i-1]
    end

    puts one
    puts "\n"
    puts two
    puts "\n"
    puts three

    And this appeared on the Console ( I do not understand, I thought I should
    get 1 2 3 4 5 1 2 3 4 5 1 3 6 10 15 )

    Get length of number series:
    5

    1
    3
    6
    10
    15

    1
    3
    6
    10
    15

    1
    3
    6
    10
    15


    David Guest

  2. #2

    Default Re: I don't understand this behavour, is it a bug?

    In ruby, as opposed to say basic or c, the variables do not hold values but
    references to the values. In c terms all ruby variables are pointers.

    Thus one is a pointer to the data [1,2,3,4,5], it does not hold the actual
    data just the address of where it is.

    When you do

    two = one

    you are copying the address of the data from one to two. one and two (and
    three) are now pointing to the same data.

    So when you change the data via the address given in the variable three one
    and two are still pointing to the same data and therefore show the same values.

    The key phrase here is 'variables store a reference to the data and not the
    data itself'.

    I hope I haven't confused you too much.



    Peter Guest

  3. #3

    Default Re: I don't understand this behavour, is it a bug?

    On Fri, Jan 09, 2004 at 12:28:38AM +0900, David Milne wrote: 
     

    No, it's not a bug. In Ruby and most OO languages, variables don't
    contain the actual objects; they contain references or pointers to the
    objects. So when you make an assignment, the reference is copied, but it's
    still referencing the same actual object. You wind up with multiple variables
    pointing to the same object.

    If you want to make a duplicate of an object, you have to do so explicitly
    by calling the clone method, e.g.

    one = [1, 2, 3, 4, 5]
    two = one.clone
    three = one.clone

    -Mark
    Mark Guest

  4. #4

    Default Re: I don't understand this behavour, is it a bug?


    "Mark J. Reed" <com> wrote in message
    news:thereeds.org... [/ref]
    it a 
    > [/ref]
    two [/ref]
    give [/ref]
    variables [/ref]
    two 
    >
    > No, it's not a bug. In Ruby and most OO languages, variables don't
    > contain the actual objects; they contain references or pointers to the
    > objects. So when you make an assignment, the reference is copied, but[/ref]
    it's 
    variables 

    Thanks very much Peter and Mark. I got the business about the pointers and
    the clone method. I had been starting to feel real uptight :-)

    David


    David Guest

Similar Threads

  1. Strange behavour with SqlMembershipProvider and impersonation
    By google@grahamd.info in forum ASP.NET Security
    Replies: 5
    Last Post: February 9th, 12:09 PM
  2. Strange Behavour
    By Sergei_T in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: October 20th, 07:34 AM
  3. custom made behavour
    By sanjG webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 1
    Last Post: January 7th, 12:40 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139