From: SpringFlowers AutumnMoon Date: 2007-09-30T09:09:36+09:00 Subject: Re: a = Dog.new # a is not a pointer and not a reference? Austin Ziegler wrote: > > A variable in Ruby isn't like Java, PHP, C++, C, or even Pascal. Doesn't a Ruby variable behave the same way in Java, PHP5, C++, and Python? As long as I view Ruby as a pointer to an object, everything clicks. Except until it is the C++ int &i = a or PHP's int $a =& $b then things start to get weird.. they call it reference... and it behaves like this: name = "I'm $i!\n"; } } $a = new Dog("lulu"); # a is a reference to "lulu" object $c = $a; # c is a reference to "lulu" object $b = &$a; # b is a reference to reference ? $b = new Dog("woofy"); echo "This is PHP ", phpversion(), "\n\n"; print_r($a); print_r($b); print_r($c); ?> C:\rails\depot>php assign2.php This is PHP 5.2.4 Dog Object ( [name] => I'm woofy! ) Dog Object ( [name] => I'm woofy! ) Dog Object ( [name] => I'm lulu! ) First of all, we don't have things like $b =& $a in Ruby, right? So in Ruby, we are very simple and clear. Second, the PHP code above, $b is a reference (to reference), you see when you change $b, you are changing the thing it is referring to. In this case, changing where $a is referring to. There is no way you just ask $b to refer some where else unless you use the unset() function. Maybe in Ruby, a = Dog.new("lulu") we call a as a reference... then we can't really let "a" reference another object like using a = Dog.new("woofy"). However, if you don't say a is a reference, but a "contains" a reference, then it might be possible... then you can let a contain a different reference. I think in Ruby, if we say "a" is a pointer, i can understand that. if we say "a" is a reference, then i can't understand it, as a reference in the computer science field is that you can't change where it points to or refers to. you always have to change the object where it refers to. but if you say "a" contains a reference... then i think that's fine, because "a" can contain a reference but it can contain another reference, and it is pointing else where. somehow, i think calling int &i = a in C++ or $a =& $b in PHP as reference is somewhat confusing, as people tend to think reference meaning a pointer beforehand. If we call int& i = a in C++ and $a =& $b just alias, then things are more clear. Put it this way, if those things are called alias, then will we say in Ruby, a = Dog.new("lulu") as an "alias" to the object lulu? or will we call a "a reference" to the object lulu. -- Posted via http://www.ruby-forum.com/.