Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > a = Dog.new # a is not a pointer and not a reference?

Reply
Thread Tools

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

 
 
Austin Ziegler
Guest
Posts: n/a
 
      09-29-2007
On 9/29/07, Morton Goldberg <(E-Mail Removed)> wrote:
> On Sep 29, 2007, at 1:16 PM, SpringFlowers AutumnMoon wrote:


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
--
Austin Ziegler * http://www.velocityreviews.com/forums/(E-Mail Removed) * http://www.halostatue.ca/
* (E-Mail Removed) * http://www.halostatue.ca/feed/
* (E-Mail Removed)

 
Reply With Quote
 
 
 
 
Austin Ziegler
Guest
Posts: n/a
 
      09-29-2007
On 9/29/07, Julian Tarkhanov <(E-Mail Removed)> wrote:
> From there comes the Object#replace method.


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

-austin
--
Austin Ziegler * (E-Mail Removed) * http://www.halostatue.ca/
* (E-Mail Removed) * http://www.halostatue.ca/feed/
* (E-Mail Removed)

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      09-29-2007
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

 
Reply With Quote
 
Morton Goldberg
Guest
Posts: n/a
 
      09-29-2007
On Sep 29, 2007, at 4:13 PM, Austin Ziegler wrote:

> On 9/29/07, Morton Goldberg <(E-Mail Removed)> wrote:


<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



 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      09-29-2007
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!

 
Reply With Quote
 
Julian Tarkhanov
Guest
Posts: n/a
 
      09-29-2007

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.


Sure. Mistake on my part. But the semantics of something#replace are
different than an assignment to a variable, that's what I meant.
--
Julian 'Julik' Tarkhanov
please send all personal mail to
me at julik.nl



 
Reply With Quote
 
SpringFlowers AutumnMoon
Guest
Posts: n/a
 
      09-30-2007
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/.

 
Reply With Quote
 
SpringFlowers AutumnMoon
Guest
Posts: n/a
 
      09-30-2007
SpringFlowers AutumnMoon wrote:

> $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");



actually, i think it would be better to name them differently:

$a = new Dog("lulu"); # a is a reference to "lulu" object
$c = $a; # c is a reference to "lulu" object
$b = &$a; # b is an alias to reference ?
$b = new Dog("woofy");

or

$a = new Dog("lulu"); # a is a pointer to "lulu" object
$c = $a; # c is a pointer to "lulu" object
$b = &$a; # b is a reference to pointer ?
$b = new Dog("woofy");

as they are different things. before, i tend to think of alias as just
a pointer, but people in C++ and Java told me i can't, because pointer
can point any where, but an alias cannot. we cannot change where an
alias points to, and if we change anything, it will always be changing
the thing where it points to.

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.

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

 
Reply With Quote
 
Morton Goldberg
Guest
Posts: n/a
 
      09-30-2007
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.

Try this. Without going into implementation details, of which I am
ignorant, a Ruby variable establishes an association between an
identifier and an object[*]. The association is specific to a
particular lexical scope and to a particular execution extent.
Therefore the same identifier can be associated (refer to) different
objects in different scopes and different objects at different points
of the execution. Such an association should perhaps more strictly be
called a binding rather than a reference, but 'reference' is often
used in informal discourse.

This the way I think of it. It's an old Lisp programmer's point of
view, but I think it's a reasonably good model for Ruby. It has
worked well for me -- kept me out of trouble. And if I'm guilty of
wrongly imposing Lisp semantics on Ruby, I'm sure I'll be corrected

Regards, Morton
[*] Yes, I know this is not the whole story. I'm trying to keep
things simple.

 
Reply With Quote
 
SpringFlowers AutumnMoon
Guest
Posts: n/a
 
      09-30-2007
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?

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments