Re: a = Dog.new # a is not a pointer and not a reference?
- From: Joshua Ballanco <jballanc@xxxxxxxxx>
- Date: Sun, 30 Sep 2007 19:55:53 +0900
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/.
.
- Follow-Ups:
- Re: a = Dog.new # a is not a pointer and not a reference?
- From: Joshua Ballanco
- Re: a = Dog.new # a is not a pointer and not a reference?
- References:
- a = Dog.new # a is not a pointer and not a reference?
- From: SpringFlowers AutumnMoon
- Re: a = Dog.new # a is not a pointer and not a reference?
- From: Morton Goldberg
- Re: a = Dog.new # a is not a pointer and not a reference?
- From: Austin Ziegler
- Re: a = Dog.new # a is not a pointer and not a reference?
- From: SpringFlowers AutumnMoon
- Re: a = Dog.new # a is not a pointer and not a reference?
- From: SpringFlowers AutumnMoon
- Re: a = Dog.new # a is not a pointer and not a reference?
- From: Morton Goldberg
- Re: a = Dog.new # a is not a pointer and not a reference?
- From: SpringFlowers AutumnMoon
- Re: a = Dog.new # a is not a pointer and not a reference?
- From: Morton Goldberg
- Re: a = Dog.new # a is not a pointer and not a reference?
- From: SpringFlowers AutumnMoon
- a = Dog.new # a is not a pointer and not a reference?
- Prev by Date: Re: Iconv hangs while converting chinese UTF-8 to ascii, please help.
- Next by Date: Re: a = Dog.new # a is not a pointer and not a reference?
- Previous by thread: Re: a = Dog.new # a is not a pointer and not a reference?
- Next by thread: Re: a = Dog.new # a is not a pointer and not a reference?
- Index(es):
Relevant Pages
|