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/.

.



Relevant Pages

  • Re: Problem with multiple inheritance
    ... // here myObject is a pointer of type A* that actually ... The myObject pointer could point to some ... This is not quite as simple as the RTTI example given by others, ... Cast to ABC* not to C*. ...
    (microsoft.public.vc.mfc)
  • Re: Getting An Object From A Vector Without Casting
    ... keeping clear when, in a language like Java, I'm working with a pointer ... MyObject myObj = new MyObject; ... as that implies that i is a pointer or a reference. ...
    (comp.lang.java.programmer)
  • Re: Why doesnt Ruby have pointers?
    ... You cannot do anything complicate with C++ unless you use pointer ... Without closure or nested function, if you have a variable and you want ... In ruby the ruby design will change to using closure ... I don't know much about Boost, but it seems that they enabled ...
    (comp.lang.ruby)
  • Objects -- Instances vs. References
    ... MyObject myObj = new MyObject; ... that the compiler would just keep track of the reference and not need to ... even add another pointer -- just keep using the first one. ...
    (comp.lang.java.programmer)
  • Re: what is the this keyword
    ... are blocks of memory on the memory heap and you use a variable name to ... "this" gives you a pointer to the current object ... So, say the current object is myObject of class TheObject, ... If "color" were an accessible element of the 'TheObject' class, then 'newObject' and 'myObject' will both point to the exact same object, and the assignment of the attribute through 'newObject' will be visible through 'myObject'. ...
    (comp.lang.java.help)