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

when we say

a = Dog.new("lulu")

Now a is not really a pointer, because we don't need to dereference it
to use it, like

(*a).color = "red"
a->color = "red"

When we use a.color, it is like a reference in C++ implicitly
dereference it and use its attributes.

But then a is not really a reference (like C++), because we can say

a = nil or a = Dog.new("woofy")

and now a points to some where else. With reference, once a reference
is set, it cannot point to some where else (in C++).

So it is kind of a mixture of pointer and reference?

Or, we can think of it as a pointer, and then think of "." as the "->"
in C++.

In that case, we can say that a is a pointer and not a reference.

And it seems the same way in Java, Python, and PHP5.

(Pointers and References discussed in


)

···

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

I think it best to think of a Ruby variable as holding a reference to an object. Ruby's reference semantics are different from C++'s, but IMO more mainstream. C++'s reference semantics are peculiar, to say the least, and perhaps even unique [*]. Ruby's variable semantics are simple and clean when compared to C++, so I recommend forgetting about making such comparisons.

Regards, Morton

[*] Betrand Meyer of Eiffel fame has often made fun of C++'s reference semantics. He has claimed they are beyond the understanding of mere mortals. He is joking, of course.

···

On Sep 29, 2007, at 1:16 PM, SpringFlowers AutumnMoon wrote:

when we say

a = Dog.new("lulu")

Now a is not really a pointer, because we don't need to dereference it
to use it, like

(*a).color = "red"
a->color = "red"

When we use a.color, it is like a reference in C++ implicitly
dereference it and use its attributes.

But then a is not really a reference (like C++), because we can say

a = nil or a = Dog.new("woofy")

and now a points to some where else. With reference, once a reference
is set, it cannot point to some where else (in C++).

So it is kind of a mixture of pointer and reference?

Or, we can think of it as a pointer, and then think of "." as the "->"
in C++.

In that case, we can say that a is a pointer and not a reference.

And it seems the same way in Java, Python, and PHP5.

SpringFlowers AutumnMoon wrote:

With reference, once a reference
is set, it cannot point to some where else (in C++).

Sort of sounds like a constant pointer, doesn't it?

···

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

Hi --

when we say

a = Dog.new("lulu")

Now a is not really a pointer, because we don't need to dereference it
to use it, like

(*a).color = "red"
a->color = "red"

When we use a.color, it is like a reference in C++ implicitly
dereference it and use its attributes.

But then a is not really a reference (like C++), because we can say

a = nil or a = Dog.new("woofy")

and now a points to some where else. With reference, once a reference
is set, it cannot point to some where else (in C++).

So it is kind of a mixture of pointer and reference?

It's a reference; at least, that's the term I've always heard Matz and
others use, never 'pointer'. When you do this:

   d = Dog.new("lulu")

you have assigned a reference to the dog object to the variable d. As
you mention, you don't have to dereference it; you can send messages
to an object reference, and they'll go to the object:

   d.bark # sending message 'bark' to the Dog object

When you then do this:

   d = nil

you're starting again, re-using 'd' as an identifier. There's
absolutely no connection to the previous use of 'd'.

Or, we can think of it as a pointer, and then think of "." as the "->"
in C++.

I would try to think entirely in Ruby. It never quite works to
transliterate one language's semantics into another's.

The '.' means: send the following message to the object on the dot's
left, where the object can be a literal, a variable, or a method call:

   a = "abc"
   a.upcase
   "abc".upcase
   def d
     Dog.new("lulu")
   end
   d.bark

In that case, we can say that a is a pointer and not a reference.

I wouldn't say it's a pointer. "Reference" is the universal term, as
far as I've heard.

One thing to keep in mind about objects and references in Ruby is that
every reference to an object is exactly one step away from the object,
and there are no references to references. So you never get into
pointer arithmetic and chaining.

David

···

On Sun, 30 Sep 2007, 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!

This has been discussed at greatest length imaginable. Personally I use the somwehat unacademic
"a direct reference to an object in the heap" for variables in Ruby. Whereby the reference is just that -
a handle, not a value in itself (some address of something or other). The only moment when it is handy to knowis when doing assignment.

boo = Dog.new # this makes a new Dog in the heap and saves it's "handle" in boo
boo = nil # this wil NOT replace the new Dog in the heap with a nil, but will link boo to the single instance of NilClass - the dog will stay in the heap and will eventually
# be GCed

From there comes the Object#replace method.

For the rest you can more-less safely assume that everything happens by reference.

···

On 29-sep-2007, at 19:16, SpringFlowers AutumnMoon wrote:

In that case, we can say that a is a pointer and not a reference.

--
Julian 'Julik' Tarkhanov
please send all personal mail to
me at julik.nl

* SpringFlowers AutumnMoon (2007-09-29) schrieb:

when we say

a = Dog.new("lulu")

Now a is not really a pointer,

»a« is a variable. A variable is never a pointer.

A variable is always a name for something in some scope.

In C and C++ a variable is a name for a location in memory. Variables
have a type that tells the C Compiler what to do when the variable is
used. [1]

In Ruby a variable is a name for an object. You can call this a
reference but just as well stick to »variable«.

When a variable name is used as an expression, the expression's value is
the object the variable is a name for.

When we use a.color, it is like a reference in C++ implicitly
dereference it and use its attributes.

Ruby is not an alternate syntax for C++.

mfg, simon .... l

[1] I am a human. I don't quite know what a C++ reference is.

Yes! You're right.
It's really not "reference" or "value" or "pointer" in c++ sense.

Some body calls it "reference to object" or something else.
Anyway, if you want to use conceptions in c++ sense to describe it.
It works like (i said somewhere before)....
when it's used as a left value, it works like a pointer, which means
the variable doesn't have the object. it only points to the object.
when it's used as a right value, it dereference automatically.

···

On Sep 30, 1:16 am, SpringFlowers AutumnMoon <summercooln...@gmail.com> wrote:

when we say

a = Dog.new("lulu")

Now a is not really a pointer, because we don't need to dereference it
to use it, like

(*a).color = "red"
a->color = "red"

When we use a.color, it is like a reference in C++ implicitly
dereference it and use its attributes.

But then a is not really a reference (like C++), because we can say

a = nil or a = Dog.new("woofy")

and now a points to some where else. With reference, once a reference
is set, it cannot point to some where else (in C++).

So it is kind of a mixture of pointer and reference?

Or, we can think of it as a pointer, and then think of "." as the "->"
in C++.

In that case, we can say that a is a pointer and not a reference.

And it seems the same way in Java, Python, and PHP5.

(Pointers and References discussed in

http://en.wikipedia.org/wiki/Reference_(computer_science)http://en.wikipedia.org/wiki/Reference_(C%2B%2B)

)
--
Posted viahttp://www.ruby-forum.com/.

Morton Goldberg wrote:

···

On Sep 29, 2007, at 1:16 PM, SpringFlowers AutumnMoon wrote:

I think it best to think of a Ruby variable as holding a reference to
an object. Ruby's reference semantics are different from C++'s, but
IMO more mainstream. C++'s reference semantics are peculiar, to say
the least, and perhaps even unique [*]. Ruby's variable semantics are
simple and clean when compared to C++, so I recommend forgetting
about making such comparisons.

hm... so you mean best to think of a Ruby variable as holding a pointer
to an object? I hope either

1) we use the word "reference" to mean a pointer
2) or, we just use the word pointer instead,

that's because "reference" seems to mean something different in both
C++, PHP, and in the general computer science area.

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

David A. Black wrote:

I wouldn't say it's a pointer. "Reference" is the universal term, as
far as I've heard.

What language is ruby written in? (Not jRuby or IronRuby.)

···

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

Please forget anything and everything you ever learned about pointers
and references with respect to Ruby. It simply doesn't apply. This is a
good thing.

when we say

a = Dog.new("lulu")

Now a is not really a pointer, because we don't need to dereference
it to use it, like

No, it isn't a pointer. Period. I'm going to introduce a new concept to
you in a moment.

(*a).color = "red"
a->color = "red"

When we use a.color, it is like a reference in C++ implicitly
dereference it and use its attributes.

But then a is not really a reference (like C++), because we can say

a = nil or a = Dog.new("woofy")

and now a points to some where else. With reference, once a
reference is set, it cannot point to some where else (in C++).

No, that's not true. You can, with some difficulty, rereference.

So it is kind of a mixture of pointer and reference?

Nope.

Or, we can think of it as a pointer, and then think of "." as the
"->" in C++.

No, you can't.

In that case, we can say that a is a pointer and not a reference.

No, you can't.

And it seems the same way in Java, Python, and PHP5.

I can't speak toward Python, but Java and PHP treat variables
differently than Ruby.

A variable in Ruby isn't like Java, PHP, C++, C, or even Pascal.

I think it best to think of a Ruby variable as holding a reference to
an object. Ruby's reference semantics are different from C++'s, but
IMO more mainstream. C++'s reference semantics are peculiar, to say
the least, and perhaps even unique [*]. Ruby's variable semantics are
simple and clean when compared to C++, so I recommend forgetting
about making such comparisons.

No, it's best to forget the concept of a Ruby variable as a shoebox. It
doesn't *hold* anything.

In C/C++, a variable is a shoebox. This shoebox has a physical location
and dimension (the address in memory). One shoebox can hold the location
of another shoebox. That's a pointer or a reference (under the covers,
they are the same in C++).

A variable in Ruby is a sticky note (like a Post-It). You can put that
sticky note on an object, or you can put multiple sticky notes on an
object. But if you move the sticky note, you're changing the object to
which it references. Since they're labels, the sticky notes don't
contain anything -- they just name the object that they're attached to.
Since they don't contain anything, no other sticky note can point to
another sticky note.

A Ruby variable is nothing like a C++ variable. Never has been, never
will be.

(Don't think that Symbols are special, either. They're not. Ruby just
keeps a list of all sticky notes that were ever created so you can,
within a certain scope and context, see if the objects you want know
anything about those particular sticky notes. It's sort-of a master
index, that way. But it's not magic. It's how you use them that's
magic.)

[*] Betrand Meyer of Eiffel fame has often made fun of C++'s reference
semantics. He has claimed they are beyond the understanding of mere
mortals. He is joking, of course.

No, he's not. Bjarne is clearly not a mere mortal. Or from the upper
planes.

-austin

···

On 9/29/07, Morton Goldberg <m_goldberg@ameritech.net> wrote:

On Sep 29, 2007, at 1:16 PM, SpringFlowers AutumnMoon 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

There is no Object#replace method. String, Array, and Hash each have
#replace methods, but Object does not.

-austin

···

On 9/29/07, Julian Tarkhanov <listbox@julik.nl> wrote:

From there comes the Object#replace method.

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

I spoke with Dr. Meyer during his trip to China last year. One of the
things we touched upon (naturally) was programming language design. He
is not joking when he talks about C++'s reference semantics. Not in the
slightest. Don't mistake his general good cheer and good nature for not
vociferously hating that language. :wink:

···

On Sun, 2007-30-09 at 02:54 +0900, Morton Goldberg wrote:

[*] Betrand Meyer of Eiffel fame has often made fun of C++'s
reference semantics. He has claimed they are beyond the understanding
of mere mortals. He is joking, of course.

--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
Never, ever, ever let systems-level engineers do human interaction
design unless they have displayed a proven secondary talent in that
area. Their opinion of what represents good human-computer interaction
tends to be a bit off-track. (Bruce Tognazzini)

7stud -- wrote:

David A. Black wrote:

I wouldn't say it's a pointer. "Reference" is the universal term, as
far as I've heard.

What language is ruby written in? (Not jRuby or IronRuby.)

C

···

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/\]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618\]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html\]

I think that 'pointer', for most programmers, means an explicit memory
address. Ruby references should not be understood as explicit memory
addresses but instead as opaque values that are processed by the
underlying Ruby implementation as necessary to locate or 'reference'
the associated object.

At the level of a Ruby programmer I think it is best to discard the
idea of 'pointer' entirely and think entirely about references to
objects. At the level of a Ruby language implementor it becomes
necessary at some point to discuss how a Ruby reference can be
converted to a pointer to a chunk of memory, but that is an implementation
detail that shouldn't concern a Ruby application programmer.

Gary Wright

···

On Sep 29, 2007, at 2:12 PM, SpringFlowers AutumnMoon wrote:

Morton Goldberg wrote:

On Sep 29, 2007, at 1:16 PM, SpringFlowers AutumnMoon wrote:

I think it best to think of a Ruby variable as holding a reference to
an object. Ruby's reference semantics are different from C++'s, but
IMO more mainstream. C++'s reference semantics are peculiar, to say
the least, and perhaps even unique [*]. Ruby's variable semantics are
simple and clean when compared to C++, so I recommend forgetting
about making such comparisons.

hm... so you mean best to think of a Ruby variable as holding a pointer
to an object? I hope either

1) we use the word "reference" to mean a pointer
2) or, we just use the word pointer instead,

Austin Ziegler wrote:
...

No, it's best to forget the concept of a Ruby variable as a shoebox. It
doesn't *hold* anything.

In C/C++, a variable is a shoebox. This shoebox has a physical location
and dimension (the address in memory). One shoebox can hold the location
of another shoebox. That's a pointer or a reference (under the covers,
they are the same in C++).

A variable in Ruby is a sticky note (like a Post-It). You can put that
sticky note on an object, or you can put multiple sticky notes on an
object. But if you move the sticky note, you're changing the object to
which it references. Since they're labels, the sticky notes don't
contain anything -- they just name the object that they're attached to.
Since they don't contain anything, no other sticky note can point to
another sticky note.

A Ruby variable is nothing like a C++ variable. Never has been, never
will be.

It's not so mysterious. If you're coming from C/C++, you may find it helpful to think of a ruby variable as a C void* variable. Assigning between variables in ruby is just like C assignment between two void* vars.

In many cases, the void* points to a struct with the data of the object, including some flags, a pointer to the class of the object, a pointer to the table of instance variables, and so on. Assignment copies the void* pointer (4 bytes), not the struct (20+ bytes).

In a few cases, the void* isn't really a pointer, but an immediate value: a fixnum, boolean, or symbol. In those cases, though, you can just as well think of it as a pointer to a unique instance of the number 42, or whatever. At least, that's a useful fiction when you're writing ruby code, but not when you're writing an extension in C.

If you're not coming from C/C++, the following metaphor (maybe a variation on the sticky metaphor, but I like it better) may help:

On a piece of paper, draw a vertical line. On the left of the line, write down some variable names:

  x
  y
  z

On the right of the line, write down some values:

  1
  7.2
  false
  "hello, world"
  [ [1, 0], [0, 1] ]

Now, draw some arrows from left to right, connecting names with values. Make sure that each name has only one arrow coming out of it. (Those arrows are together called a binding.) The things on the right have a physical location in memory. The things on the left do too, but in a more subtle way.

What the assignment "y = x" means is: look at the value that the arrow from x leads to, then erase the arrow that starts from y (if any), and draw a new arrow from y to the same value that's connected to x. You should now have one arrow from x and one arrow from y, both leading to the same value on the right.

If you keep this diagram in mind, you'll understand why the following happens in ruby:

   x = [2,3]
   y = x
   x[1] = 4
   p y[1] # ==> 4

and you'll also understand why

   x = 0
   x++

can't make any sense.

This arrow diagram isn't the whole picture of course: eval breaks down the wall between variable name and value, and there are different bindings in different scopes. But there's nothing fundamentally wrong with this picture.

···

--
        vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

<snip>

A variable in Ruby isn't like Java, PHP, C++, C, or even Pascal.

I think it best to think of a Ruby variable as holding a reference to
an object. Ruby's reference semantics are different from C++'s, but
IMO more mainstream. C++'s reference semantics are peculiar, to say
the least, and perhaps even unique [*]. Ruby's variable semantics are
simple and clean when compared to C++, so I recommend forgetting
about making such comparisons.

My use of "holding" may be unfortunate. I only meant it conceptually. I was trying to make things simple for the OP.

No, it's best to forget the concept of a Ruby variable as a shoebox. It
doesn't *hold* anything.

In C/C++, a variable is a shoebox. This shoebox has a physical location
and dimension (the address in memory). One shoebox can hold the location
of another shoebox. That's a pointer or a reference (under the covers,
they are the same in C++).

A variable in Ruby is a sticky note (like a Post-It). You can put that
sticky note on an object, or you can put multiple sticky notes on an
object. But if you move the sticky note, you're changing the object to
which it references. Since they're labels, the sticky notes don't
contain anything -- they just name the object that they're attached to.
Since they don't contain anything, no other sticky note can point to
another sticky note.

By "sticky notes" you seem to be talking about bindings, like a Lisp variable makes. That is how I think of Ruby variables, myself, but I didn't think bringing bindings up would help the OP. I didn't think of the analogy with sticky notes. I like that -- I'll have to remember it.

A Ruby variable is nothing like a C++ variable. Never has been, never
will be.

We are in complete agreement. That was the point I trying to make. Did I phrase it so obscurely that you took me to be saying the opposite?

[*] Betrand Meyer of Eiffel fame has often made fun of C++'s reference
semantics. He has claimed they are beyond the understanding of mere
mortals. He is joking, of course.

That should be Bertrand not Betrand. Pardon my typo.

No, he's not. Bjarne is clearly not a mere mortal. Or from the upper planes.

Are you implying Bjarne Stroustrup is some kind of demon? He always struck me as a very nice person.

Regards, Morton

···

On Sep 29, 2007, at 4:13 PM, Austin Ziegler wrote:

On 9/29/07, Morton Goldberg <m_goldberg@ameritech.net> wrote:

Hi --

···

On Sun, 30 Sep 2007, 7stud -- wrote:

David A. Black wrote:

I wouldn't say it's a pointer. "Reference" is the universal term, as
far as I've heard.

What language is ruby written in? (Not jRuby or IronRuby.)

C. It sounds like you're asking the question rhetorically, though. I'm
not sure what you mean.

David

--
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!

Sure. Mistake on my part. But the semantics of something#replace are different than an assignment to a variable, that's what I meant.

···

On 29-sep-2007, at 22:15, Austin Ziegler wrote:

There is no Object#replace method. String, Array, and Hash each have
#replace methods, but Object does not.

--
Julian 'Julik' Tarkhanov
please send all personal mail to
me at julik.nl

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:

<?php

class Dog {
    var $name;

    function Dog($i) {
        $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/\.

Just out of curiosity, is this the Dragomir Meyer who wrote "50 pitfalls in
C++"? (The only book I ever read about C++, but it was worth it's money
because it made me stay away!)

Thx
Robert

···

On 9/30/07, Michael T. Richter <ttmrichter@gmail.com> wrote:

On Sun, 2007-30-09 at 02:54 +0900, Morton Goldberg wrote:

[*] Betrand Meyer of Eiffel fame has often made fun of C++'s reference semantics. He has claimed they are beyond the understanding of mere mortals. He is joking, of course.

I spoke with Dr. Meyer during his trip to China last year. One of the
things we touched upon (naturally) was programming language design. He is
not joking when he talks about C++'s reference semantics. Not in the
slightest. Don't mistake his general good cheer and good nature for not
vociferously hating that language. [image: ;)]

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