[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

SpringFlowers AutumnMoon

9/29/2007 5:17:00 PM

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_%28computer_...
http://en.wikipedia.org/wiki/Reference_%28...

)
--
Posted via http://www.ruby-....

58 Answers

Morton Goldberg

9/29/2007 5:54:00 PM

0

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.

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.

7stud 7stud

9/29/2007 6:08:00 PM

0

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

David A. Black

9/29/2007 6:10:00 PM

0

SpringFlowers AutumnMoon

9/29/2007 6:12:00 PM

0

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

7stud 7stud

9/29/2007 6:39:00 PM

0

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

Tim Hunter

9/29/2007 6:43:00 PM

0

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/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

Gary Wright

9/29/2007 6:47:00 PM

0


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,

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


John Joyce

9/29/2007 7:23:00 PM

0


On Sep 29, 2007, at 1:46 PM, Gary Wright wrote:

>
> 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,
>
> 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
>
>
Yep.
You're not guaranteed (AFAIK) any direct memory access in Ruby, nor
should you be concerned about it. It is Ruby. The goal is to allow
you to focus on programming and making things happen. If you want
access and control at a lower level, you'll need to use a language
like C or C++ or perhaps Objective-C (don't know if Obj-C 2.0, coming
in a month or so, will allow such low level access)
In Ruby just think about identifiers as references to objects. The
only case that requires a little time to get a hang of is Ruby's
Symbol class. They could be described as constant references (for the
life of the program) and should be used carefully like globals. You
don't want too many of them, but with any modern system you should
have reasonable amount of headroom, unless you're dealing with a huge
data set.
With Ruby, like SmallTalk, you're pretty much always dealing with
objects and nothing much lower-level like pointers or primitive types.

Julian 'Julik' Tarkhanov

9/29/2007 7:29:00 PM

0


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.

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.
--
Julian 'Julik' Tarkhanov
please send all personal mail to
me at julik.nl



Austin Ziegler

9/29/2007 8:13:00 PM

0

On 9/29/07, Morton Goldberg <m_goldberg@ameritech.net> 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 * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca