A = Dog.new # a is not a pointer and not a reference?

Because I tend to assume the best intentions in people. I see no
reason to assume otherwise.

-austin

···

On 9/30/07, 7stud -- <dolgun@excite.com> wrote:

Austin Ziegler wrote:
> I've seen you ask similar questions on three threads and you're no
> closer to understanding
What makes you think that is the op's goal?

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

7stud -- wrote:

Austin Ziegler wrote:

I've seen you ask similar questions on three threads and you're no
closer to understanding

What makes you think that is the op's goal?

I asked similar question before although at that time I didn't know the
word reference can mean "alias that can't change where it points to" in
C++ and the general CS field. the word we in use in Ruby is also
reference. so trying to clarify how it relates to the rest of the
world.

···

--
Posted via http://www.ruby-forum.com/\.

There is a lot of confusion on this one. While I know for sure that
there are low level compiled languages that will allow the pointers to
be explicitly OR implicitly called (i.e. you are not *required* to use
the dereference) I do not believe that an interpreted language would
employ specific pointers.

That having been said, what is everyone basing their positions upon? Is
this more of a debate about what things are or how to word them to
others? Where did everyone look in the docs or source code to figure
this out?

···

--
Posted via http://www.ruby-forum.com/.

Austin Ziegler wrote:

No, I don't think that's a valid comparison because a void* still
takes up space -- it's a location -- in C++. That is, I can
legitimately do:

  void* a = &5;
  void* b = &a;

It's *important* for people to understand deeply that Ruby variables
take up no space -- they're labels. If I call something a cat and you
call that same something 'Fluffy', we're still referring to the same
animal, but neither of our names for that animal can easily be
referred to by another name.

Ruby variables are "wafer-thin". They're, as I said, just sticky notes
that can be moved around at will. It's the *objects* that take up the
space.

Ruby variables take up just as much space, they just do it in the call
stack rather than on the heap.

That's an implementer's view. When I program with Ruby, I don't think of
a variable taking up space. In purely Ruby terms, a variable isn't an
object that I can manipulate. In C++ terms, I can manipulate a variable
because it's a concrete item.

Ruby variables hold the value of an object reference. If you re-assign
them, you are setting them to a different object reference. If you
retrieve them, you're getting an object reference which you can then
pass by value or use to invoke methods.

Conceptually, they're better seen as labels or sticky notes. Maybe not
for the implementers, but definitely at the normal programmer's level.
Thinking about them more than that is just asking for madness because
you can't interact with them at any other level than that.

-austin

···

On 9/30/07, Charles Oliver Nutter <charles.nutter@sun.com> wrote:
--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

Charles Oliver Nutter wrote:

Ruby variables hold the value of an object reference. If you re-assign
them, you are setting them to a different object reference. If you
retrieve them, you're getting an object reference which you can then
pass by value or use to invoke methods.

yes, that's what i mean by thinking of a reference to an object as a
pointer. let's say if we implement Ruby, can't we actually set it as a
pointer to the Dog object, and then whenever we use d.bark(), it will go
to where it points to and start looking for a table of method names.
and if not found, go to the parents, again using a pointer to the parent
class. i won't increment the pointer or do pointer arithmetic, and i
won't care what the pointer's value is (the address) and won't tell the
programmer, but underneath, it is still a pointer.

so

d = Dog.new("lulu") # d points to some new memory chunk
b = d # b points to the same place
d = Dog.new("woofy") # d now points to a new memory chunk
                          # and b points to the old chunk

···

--
Posted via http://www.ruby-forum.com/\.

Well, your assertion that a.value is the equivalent of a->value is a good example of getting into trouble by equating a reference to a pointer. The expression a.value is a method call, not a value fetch. For a.value to work, the object referred by a has have a method called 'value', either an instance method or singleton method, in its extended object table. If it does, the method is executed and some object is returned. That's not at all what a->value does in C.

Regards, Morton

···

On Sep 29, 2007, at 10:17 PM, SpringFlowers AutumnMoon wrote:

Morton Goldberg wrote:

On Sep 29, 2007, at 8:18 PM, SpringFlowers AutumnMoon wrote:

Come to think about it, a pointer is very clear cut... an alias is
very
clear cut. but when it is "reference", then you have to think whether
it is the pointer behavior or the alias behavior.

No, you do not. Ruby has it own semantics, which do not correspond
with the semantics of C or C++. Trying to impose the semantics of
other languages onto Ruby isn't going to work. It's best to drop such
baggage and start fresh.

hm,... but is there a problem to think of reference in Ruby as a
"pointer"? will that cause any trouble? I only see that a.value is
not the same as C and C++ would use a->value, but in Ruby we use the "."
to replace the "->" and that's it. Will there be further trouble or
discrepancy to think of Ruby reference as a pointer?

Hi --

Morton Goldberg wrote:

Come to think about it, a pointer is very clear cut... an alias is
very
clear cut. but when it is "reference", then you have to think whether
it is the pointer behavior or the alias behavior.

No, you do not. Ruby has it own semantics, which do not correspond
with the semantics of C or C++. Trying to impose the semantics of
other languages onto Ruby isn't going to work. It's best to drop such
baggage and start fresh.

hm,... but is there a problem to think of reference in Ruby as a
"pointer"? will that cause any trouble? I only see that a.value is
not the same as C and C++ would use a->value, but in Ruby we use the "."
to replace the "->" and that's it. Will there be further trouble or
discrepancy to think of Ruby reference as a pointer?

Imagine if you went to a C mailing list and announced that you were
going to call pointers "references". That's the main issue: you're
making up new terminology instead of using the terminology used by
creator of Ruby and by people who have been using and talking about
the language for years and years.

The dot in Ruby is the dot in Ruby. It's not a replacement for
anything else. It's actually pretty easy to understand; it means "send
a message to this object". There's no need to go looking for what it
would mean if this weren't Ruby.

David

···

On Sun, 30 Sep 2007, SpringFlowers AutumnMoon wrote:

On Sep 29, 2007, at 8:18 PM, SpringFlowers AutumnMoon wrote:

--
Upcoming training from Ruby Power and Light, LLC:
   * Intro to Ruby on Rails, Edison, NJ, October 23-26
   * Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

SpringFlowers AutumnMoon wrote:

hm,... but is there a problem to think of reference in Ruby as a "pointer"? will that cause any trouble? I only see that a.value is not the same as C and C++ would use a->value, but in Ruby we use the "." to replace the "->" and that's it. Will there be further trouble or discrepancy to think of Ruby reference as a pointer?

Ruby variables hold the value of an object reference. Reassigning the variable set it to a different object reference. Retrieving the value gives you an object reference you can then pass by value to another method or use to invoke methods on the object.

Pointers are lower-level. You can't do pointer math with Ruby variables, and you can never retrieve the memory address of an object, so thinking of Ruby variables as pointers is misleading.

- Charlie

I can see the value of the sticky note analogy but I think it
obscures the idea of scopes. It puts the variable name in close
proximity to the referred object and not the referring scope.

Think about instance variables, for example. You can have two
objects each with an instance variable that has a reference to
a common third object:

class Foo
   attr_accessor :other
end

alpha = Foo.new
beta = Foo.new

gamma = Object.new

alpha.other = gamma
beta.other = gamma

Does it make sense to think of gamma as having two sticky notes
with '@other' written on them? Not to me at least.

The problem with the sticky note analogy, for me, is that it implies
that the object has access to the sticky notes--that the object
can 'see' what variables are referring to itself. The notes seem
to be 'stuck' to the wrong thing in my mind. They should be more
like a piece of paper with a name and a phone number written on it.
The name is the variable and the phone number is the reference for
the other person (object). The phone system is an opaque system
that provides a way for us to contact another person and
communicate with them (send a message to another object) without
us having to know where that person is physically located.

Gary Wright

···

On Sep 30, 2007, at 10:54 PM, Austin Ziegler wrote:

Conceptually, they're better seen as labels or sticky notes. Maybe not
for the implementers, but definitely at the normal programmer's level.
Thinking about them more than that is just asking for madness because
you can't interact with them at any other level than that.

Austin Ziegler wrote:

That's an implementer's view. When I program with Ruby, I don't think of
a variable taking up space. In purely Ruby terms, a variable isn't an
object that I can manipulate. In C++ terms, I can manipulate a variable
because it's a concrete item.

Ruby variables hold the value of an object reference. If you re-assign
them, you are setting them to a different object reference. If you
retrieve them, you're getting an object reference which you can then
pass by value or use to invoke methods.

Conceptually, they're better seen as labels or sticky notes. Maybe not
for the implementers, but definitely at the normal programmer's level.
Thinking about them more than that is just asking for madness because
you can't interact with them at any other level than that.

Your original post was ridiculous and now you've been called out. Take
your medicine with some semblance of grace. Back pedaling as fast as
you can stirs up a cloud of dust, but we still know its you in there.

···

--
Posted via http://www.ruby-forum.com/\.

SpringFlowers AutumnMoon wrote:

Charles Oliver Nutter wrote:

Ruby variables hold the value of an object reference. If you re-assign
them, you are setting them to a different object reference. If you
retrieve them, you're getting an object reference which you can then
pass by value or use to invoke methods.

yes, that's what i mean by thinking of a reference to an object as a pointer. let's say if we implement Ruby, can't we actually set it as a pointer to the Dog object, and then whenever we use d.bark(), it will go to where it points to and start looking for a table of method names. and if not found, go to the parents, again using a pointer to the parent class. i won't increment the pointer or do pointer arithmetic, and i won't care what the pointer's value is (the address) and won't tell the programmer, but underneath, it is still a pointer.

I think the named slots holding object references analogy is probably as accurate as you can get without getting into implementation specifics. For example, in Ruby, the named slots *arg* pointers to objects (except in a few cases like Fixnums). In JRuby, they're just Java references, again passed-by-value object references. But named slots will apply fairly well across all implementations.

- Charlie

Hi --

There is a lot of confusion on this one. While I know for sure that
there are low level compiled languages that will allow the pointers to
be explicitly OR implicitly called (i.e. you are not *required* to use
the dereference) I do not believe that an interpreted language would
employ specific pointers.

That having been said, what is everyone basing their positions upon? Is
this more of a debate about what things are or how to word them to
others? Where did everyone look in the docs or source code to figure
this out?

I'm just following what I've always understood, from discussions with
Matz, the Pickaxe, etc., to be the standard terminology. From the
first edition of the Pickaxe:

"Ruby variables and constants hold references to objects. Variables
themselves do not have an intrinsic type. Instead, the type of a
variable is defined solely by the messages to which the object
referenced by the variable responds."

The explanation goes into more depth but that's the basics. I've never
felt the need or desire to reconcile this with C++ or anything else.
It's always seemed straightforward to me.

David

···

On Mon, 1 Oct 2007, Lloyd Linklater wrote:

--
Upcoming training from Ruby Power and Light, LLC:
   * Intro to Ruby on Rails, Edison, NJ, October 23-26
   * Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

Morton Goldberg wrote:

···

On Sep 29, 2007, at 10:17 PM, SpringFlowers AutumnMoon wrote:

with the semantics of C or C++. Trying to impose the semantics of
other languages onto Ruby isn't going to work. It's best to drop such
baggage and start fresh.

hm,... but is there a problem to think of reference in Ruby as a
"pointer"? will that cause any trouble? I only see that
a.value is
not the same as C and C++ would use a->value, but in Ruby we use
the "."
to replace the "->" and that's it. Will there be further trouble or
discrepancy to think of Ruby reference as a pointer?

Well, your assertion that a.value is the equivalent of a->value is a
good example of getting into trouble by equating a reference to a
pointer. The expression a.value is a method call, not a value fetch.
For a.value to work, the object referred by a has have a method
called 'value', either an instance method or singleton method, in its
extended object table. If it does, the method is executed and some
object is returned. That's not at all what a->value does in C.

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.

--
Posted via http://www.ruby-forum.com/\.

> Conceptually, they're better seen as labels or sticky notes. Maybe not
> for the implementers, but definitely at the normal programmer's level.
> Thinking about them more than that is just asking for madness because
> you can't interact with them at any other level than that.
I can see the value of the sticky note analogy but I think it
obscures the idea of scopes. It puts the variable name in close
proximity to the referred object and not the referring scope.

It's an analogy. Nothing more, nothing less. There are ALWAYS limits
to analogies. The limit to the sticky note analogy is that scopes are
obscured. I don't have a problem with that, because that's not the
nature of the confusion that was being exhibited. With some
mind-bending, scopes could be dealt with in the analogy (when you
enter a new scope, you put a piece of wrapping paper over the object
and you can name things...).

The problem with the sticky note analogy, for me, is that it implies
that the object has access to the sticky notes--that the object
can 'see' what variables are referring to itself.

That's not at all implied by what I said. If I were to attach a sticky
note to the middle of your back, you wouldn't have access to it. :wink:

But even if we change it to your "note with name and phone number"
concept, the important piece here is that the variable is a note, not
a container. Again, this doesn't apply to actual implementation
details, but when I'm programming in Ruby, I simply don't think about
the space a variable consumes.

-austin

···

On 9/30/07, Gary Wright <gwtmp01@mac.com> wrote:

On Sep 30, 2007, at 10:54 PM, Austin Ziegler wrote:

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

Are you always this unpleasant, boorish, and rude?

Tell me: do you really care that a Ruby variable takes up space? You,
as a programmer, shouldn't. Can you do a damned thing about it? Nope.
In the end, it doesn't matter. Charles is *technically* correct from
the implementer's perspective. Gary Wright has provided the only valid
complaint, that I ignore scope. Well, yes, I did. On purpose. I
considered how to address it and decided it was better to ignore it
for the concept.

From a Ruby programmer's perspective, it is important to understand
that a variable is a label -- a name -- for an object, and that the
name is transferable and is always transferred on assignment (of which
a special form is argument setting in method passing). It has as much
permanence as a hastily scribbled note about an object that you're
dealing with.

Anyone telling you anything else is either (a) an implementer who has
to care about the inner details or (b) confused. (BTW, understanding
variables this way also helps demystify Symbols immensely, and there's
a lot of confusion about Symbols because people want to think that
they're some magic juju.)

-austin

···

On 10/1/07, 7stud -- <dolgun@excite.com> wrote:

Austin Ziegler wrote:
> That's an implementer's view. When I program with Ruby, I don't think of
> a variable taking up space. In purely Ruby terms, a variable isn't an
> object that I can manipulate. In C++ terms, I can manipulate a variable
> because it's a concrete item.

>> Ruby variables hold the value of an object reference. If you re-assign
>> them, you are setting them to a different object reference. If you
>> retrieve them, you're getting an object reference which you can then
>> pass by value or use to invoke methods.
> Conceptually, they're better seen as labels or sticky notes. Maybe not
> for the implementers, but definitely at the normal programmer's level.
> Thinking about them more than that is just asking for madness because
> you can't interact with them at any other level than that.
Your original post was ridiculous and now you've been called out. Take
your medicine with some semblance of grace. Back pedaling as fast as
you can stirs up a cloud of dust, but we still know its you in there.

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

Yup 'cause '.' is not dereferencing period (pun intended). It is
sending a message

x.a is almost equivalent to
x.send :a.

Variables in Ruby are references, period again :wink: but you cannot
dereference them.
I guess this was somehow lost in the discussion.

HTH
R.

···

On 9/30/07, SpringFlowers AutumnMoon <summercoolness@gmail.com> 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.

--
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/

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

I think part of your confusion here, SFAM, is that you're talking about
"pointers" and "references" as if they were distinct entities. They are
not. A pointer is a kind of reference; an implementation detail, in
effect. So C, you see, has references (as do various assemblers, in the
same general form). They are called "pointers" and have some ugly
syntax and semantics:

        int a = 5; /* a is a box holding 5 */
        int *b = &a; /* b is a box that points to 5 */
        int c = a; /* c is a box holding 5 */
        int d = *b; /* d is a box holding 5 */
        a = 6; /* a now holds 6, b points to 6, c & d are boxes
        holding 5 */

Note how ugly the reference syntax is in C? You have to explicitly tell
it that it is a reference (int *b) and you have to use special syntax to
turn a variable into a reference (&a). Later, when you want to use the
value of the referent, you need, again, to explicitly say so (*b).
(Now, to be fair, there are some pretty cool tricks you can do with C's
references, but we're in the realm of using chainsaws to clip our
fingernails when we use a feature with that much power to do routine
application things.)

References in C++ are either the old-style C references (pointers) or a
newer breed of reference which they confusingly call... references. As
if they were something different. Now the syntax is a bit friendlier:

        int a = 5; // a is a box holding the value 5
        int &b = a; // b is a box holding the location of the box a
        int c = a; // c is a box holding the value 5
        int d = b; // d is a box holding the value 5
        a = 6; // a now holds 6, b points to 6, c and d & boxes
        holding 5

Note that you still have to specifically say that a variable is a
reference (int &b), but you no longer have to take addresses explicitly
(no &a) and you no longer have to tell the compiler that you mean the
value pointed to, not its location (no *b). Under the covers, though,
it's all the same thing: boxes with values and other boxes that point to
those values. There's only a few minor advantages to using references
over pointers:

     1. The cleaner syntax. (You can't make the mistake of accidentally
        using an address when you mean the value.)
     2. The lower power. (Yes, this is an advantage: you're clipping
        your nails with a belt sander instead of a chainsaw. :D)
     3. The compiler can better reason about the data involved and can
        silently optimize away the actual references if it turns out
        that using the data straight is safer and won't have any
        semantic impact.

Ruby's references are a completely different implementation. And,
indeed, Ruby's variables in general are radically different in nature.
A variable in Ruby is more like a file handle (another form of
reference, incidentally) or an array index (yet another form of
reference) than it is like a C or C++ variable (a box with a value).

        a = 5 # a.object_id is 11 in my irb session.
        b = a # a and b.object_id are both 11.
        c = a # a, b and c.object_id are all 11.
        d = b # a, b, c and d.object_id are all 11.
        a = 6 # a.object_id is now 13. b, c and d.object_id are all
        still 11.

Note that here things are radically different from C/C++ (whose
"pointers" and "references" are basically the same thing under the
covers and implemented in mostly the same way). a is not a box with a
value. a is an abstract label (value 11) that is used by the Ruby
runtime to find the value 5. You can view it as a "pointer" of sorts,
but it's probably better to think of it like you'd think of a file
handle returned from an fopen() system call: it's an opaque data
structure containing a lot of information behind the scenes, one of
which happens to be an indirect reference to the value 5. When we
assign a to b, b is now the very same thing. It's just another name, in
effect, for exactly the same object information (which happens to
indirectly reference the value 5). As we proceed to c (from a) and d
(from b), we're basically just attaching more names to the very same
information. It's sort of like doing fopen() several times on the same
file (as long as you ignore issues like seeking, etc.). At the very
end, when I assign 6 to a, what I'm really doing is making a brand new
"handle" that contains, among other things, an indirect reference to the
value 6. This does not change, in any way, the values assigned to b, c
or d. Those are still handles to the same data structure with its
indirect reference to 5.

So....

Short version: Ruby variables are all references. So are C++
"references" and C/C++ "pointers". But the Ruby implementation is
radically different from the C/C++ versions. The sooner you leave your
C/C++-derived notions of what variables "really" are and how they
"really" work behind, the sooner you will understand the subtler aspects
of Ruby's semantics.

If you're still having troubles figuring this out (or if anybody else is
quietly watching this and still getting confused), drop me a line by
private email and I'll hack together some C++ code that kinda-sorta does
the same thing as Ruby so you can understand what's going on in a
variable structure you're more familiar with.

···

On Sun, 2007-30-09 at 15:39 +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.

--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
We should sell bloat credits, the way the government sells pollution
credits. Everybody's assigned a certain amount of bloat, and if they go
over, they have to purchase bloat credits from some other group that's
been more careful. (Bent Hagemark)

<snip>

Are you always this unpleasant, boorish, and rude?

Yet another troll Austin, best to be ignored.
Cheers
Robert

···

On 10/1/07, Austin Ziegler <halostatue@gmail.com> wrote:

--
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/

Joshua Ballanco wrote:

The reason that concepts such as pointer and reference do translate well
between C++ and Ruby is because of this difference in paradigms.

Apologies. That should be "do not translate well"

···

--
Posted via http://www.ruby-forum.com/\.