From: Joshua Ballanco Date: 2007-09-30T19:55:53+09:00 Subject: Re: a = Dog.new # a is not a pointer and not a reference? SpringFlowers AutumnMoon wrote: > how about just a.set_value(3) vs a->set_value(3). what i mean is just > the "." and "->" difference. using a "." in Ruby and "->" in C or C++. > If I think of "a" as a pointer and "." as "->", will that get in trouble > and have any discrepancy for other things. I think that this will get you into trouble only because you will continue trying to see Ruby as working from the same OOP paradigm as C++. There are, actually, two (common) ways to do OOP: one comes from Simula and the other from SmallTalk. Basically, Simula-style OOP (which is what C++ uses) treats "objects" as structs of values and function pointers. When you say "myObject.doThis" you are dereferencing "myObject", which is actually a pointer to the object struct, and then invoking the function pointed to by "doThis". On the other hand, SmallTalk style OOP (which is what Ruby uses) treats objects as containers (an array or hash if you want) of values and instantiated from a class. Classes are where the functions are held. When you say "myObject.doThis", the runtime engine asks the class that "myObject" was instantiated from if it knows about a "doThis" function. If it does, then it gives the values held by "myObject" to the "doThis" function from the class. The reason that concepts such as pointer and reference do translate well between C++ and Ruby is because of this difference in paradigms. In the Simula style, "myObject" has to know where "doThis" resides in memory, hence why it is important to know if "myObject" is a pointer or a reference. In the SmallTalk style, "myObject" just has to know what class it belongs to; the runtime handles keeping track of where functions live in memory. This is why, as was said earlier, "myObject" in Ruby is nothing more than a human-readable label. Without the runtime it is meaningless. To see what this means in practice, try doing a few assignments in IRB and after each check the object_id of the variables. You'll see that it changes with reassignment. This is actually the Ruby runtime applying the labels to new objects. Hope that helps! Cheers, Joshua Ballanco -- Posted via http://www.ruby-forum.com/.