From: Charles Calvert Date: 2008-11-12T06:47:27+09:00 Subject: Re: Array variable. On Fri, 07 Nov 2008 23:33:12 +0100, Andreu wrote in : >PROBLEM SOLVED >At the beginning of my program I have written this line: > >idate = mdate = Array.new > >but it seems Ruby doesn't support this syntax (like C) and assume >that the two arrays are the same one. It's not a difference in syntax, but a difference in how variables are handled. When you write something like this in C: int a, b; a = b = 0; You are creating two variables on the stack and assigning an integer to them. The actual value 0 is stored in borth variables because they are intrinsic types that have been allocated on the stack. When you write in Ruby: idate = mdate = Array.new something different happens. Array.new returns a reference to an instance of array that has been allocated on the heap. The variable mdate holds the value of the reference, not the instance itself. This same reference is then assigned to idate. Now both variables have a reference to the same instace on the heap. A reference is really just a pointer with nicer semantics. If you're familiar with a language that has pointers, like C, then think of it like this: int * GetInt() { int * i = (int *) malloc(sizeof(int)); return i; } int * i, * j; i = j = GetInt(); Now if you modify j: *j = 5; printf("%d", *i); // prints "5" -- Charles Calvert | Web-site Design/Development Celtic Wolf, Inc. | Software Design/Development http://www.celticwolf.com/ | Data Conversion (703) 580-0210 | Project Management