[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

a different type of reference (shocked

SpringFlowers AutumnMoon

9/28/2007 4:02:00 PM

Before, when I say Ruby's reference to an object

a = Car.new
b = a

i was saying a is a reference to a Car object. and b is now the same
reference to that object.

I mean it the very traditional pointer way:

int a = 10;
int *ip, *jp;
ip = &a;
jp = ip;

Now I didn't know that, as someone told me, that there is another type
of reference in C++, Java, and PHP:


i = 10
j =& i
j = 20
// and now both i and j are 20 (!!! shocked)
// is it to think of j as jpp? a pointer to pointer to int,
// and j = 20 involves implicit deferencing? **jpp = 20
// or if it is an object, *jp = &obj ?


so I think when people talk about assignment in Python, Java, and Ruby,

a = b

is the first type of "Pointer reference"

and the second type is a "Alias reference"

Isn't that the case? Is the above true so far?

In the PHP docs, it seems they intermix the two, and talk about PHP4's
=& the same way as PHP5's $obj1 = $obj2... and that was somewhat
imprecise. In Ruby, we only have the "pointer reference" and that's it.
No need to worry about "alias" here and there.

(and in Ruby, we call a method by "pass by value, the value being the
reference (pointer) to an object). and when the method returns
something, it returns a value, which is the reference to an object.) It
is very consistent all the way. In Ruby, we don't have the "alias
reference", right?
--
Posted via http://www.ruby-....

18 Answers

Phlip

9/28/2007 10:34:00 PM

0

SpringFlowers AutumnMoon wrote:

> (and in Ruby, we call a method by "pass by value, the value being the
> reference (pointer) to an object). and when the method returns
> something, it returns a value, which is the reference to an object.) It
> is very consistent all the way. In Ruby, we don't have the "alias
> reference", right?

In Ruby, types that can (generally) fit into 32 bits are "immediate
types". These Fixnums and Floats are pass-by-value. All other types -
from String up - are pass by reference:

def foist(q)
q.replace('yo')
end
q = 'otherwise'
foist(q)
assert_equal 'yo', q

Nothing to be shocked about, because you should not scribble on your
input arguments anyway. If you need some other value inside a method,
it should get its own variable with a distinct name.

--
Phlip
http://www.oreilly.com/catalog/9780...
^ assert_xpath
http://tinyurl.... <-- assert_latest Model

Phrogz

9/29/2007 2:59:00 AM

0

On Sep 28, 4:34 pm, Phlip <phlip2...@gmail.com> wrote:
> In Ruby, types that can (generally) fit into 32 bits are "immediate
> types". These Fixnums and Floats are pass-by-value. All other types -
> from String up - are pass by reference:

But I don't think this should make any bit of difference to anyone.
Pass-by-value of an 'immediate' object is (nearly) indistinguishable
for pass-by-reference of an immutable object. The only consistent
difference that I can think of is that two literals of the same value
happen to have the same object_id. (And as has been pointed out
recently, there are very few good reasons why you should care about
object_id other than possibly debugging.)

7stud 7stud

9/29/2007 3:09:00 AM

0

SpringFlowers AutumnMoon wrote:
> Before, when I say Ruby's reference to an object
>
> a = Car.new
> b = a
>
> i was saying a is a reference to a Car object. and b is now the same
> reference to that object.
>
> I mean it the very traditional pointer way:
>
> int a = 10;
> int *ip, *jp;
> ip = &a;
> jp = ip;
>
> Now I didn't know that, as someone told me, that there is another type
> of reference in C++, Java, and PHP:
>
>
> i = 10
> j =& i
> j = 20
> // and now both i and j are 20 (!!! shocked)

Not so shocking.

int x = 10;
int* p1 = &x;
int* p2 = p1;
*p2 = 5;

cout<<*p1<<" "<<*p2<<endl; //5 5

A C++ reference, which is a different type than a pointer in C++, is
actually implemented as a pointer behind the scenes. However, C++
references allow you to use a different syntax that doesn't require
dereferencing:

int x = 10;
int& r = x; //r becomes a pointer to the same address as x
r = 5;

cout<<x<<" "<<r<<endl; //5 5

In C++, references are sometimes called 'aliases'. But ruby also has
aliases:

x = "hello"
y = x

y[0] = "H"
puts x, y //Hello Hello

In ruby, x and y are aliases for the same object, i.e. both names refer
to the same object, i.e. the object has two different names. The
difference is that the assignment operator is programmed to work
differently in the two languages.


> Isn't that the case? Is the above true so far?

No. java doesn't have pointers, and java does not have the C++
reference syntax:

int num1 = 10;
int num2 = num1;
num2 = 5;

System.out.println(num1); //10
System.out.println(num2); //5


> (and in Ruby, we call a method by "pass by value, the value being the
> reference (pointer) to an object). and when the method returns
> something, it returns a value, which is the reference to an object.) It
> is very consistent all the way.

The key to understanding the difference between pass-by-value and
pass-by-reference, in any language, is understanding that there is no
difference in the passing mechanism. Something is always copied and
sent to the method. In pass-by-value, the value itself is copied and
sent to the method, so if you change the copy from inside the method, it
does not change the original value. In pass-by-reference, the address
is copied, so if you change the value at that address from inside the
method, then the value at that address is permanently changed, and after
the method ends, the change can still be observed.

> In Ruby, we don't have the "alias
> reference", right?

Let's see:

x = "hello"
y = x

y = "goodbye"
puts x, y #hello goodbye



def change_it(num)
num += 1
end

val = 5
change_it(val)
puts val #5

What is your conclusion?
--
Posted via http://www.ruby-....

David A. Black

9/29/2007 7:11:00 AM

0

Robert Dober

9/29/2007 9:38:00 AM

0

On 9/29/07, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
> On Sat, 29 Sep 2007, Phlip wrote:
>
> > SpringFlowers AutumnMoon wrote:
> >
> >> (and in Ruby, we call a method by "pass by value, the value being the
> >> reference (pointer) to an object). and when the method returns
> >> something, it returns a value, which is the reference to an object.) It
> >> is very consistent all the way. In Ruby, we don't have the "alias
> >> reference", right?
> >
> > In Ruby, types that can (generally) fit into 32 bits are "immediate
> > types". These Fixnums and Floats are pass-by-value. All other types -
> > from String up - are pass by reference:
>
> I'd describe it more as SpringFlowers did: pass by value, where the
> value happens to be a reference.
Completely agruee with that. IIRC this discussion has been there quite
a while ago and general agreement was not reached on it.

<snip>
Robert
--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Xavier Noria

9/29/2007 10:03:00 AM

0

On Sep 29, 2007, at 11:37 AM, Robert Dober wrote:

> On 9/29/07, David A. Black <dblack@rubypal.com> wrote:
>> Hi --
>>
>> On Sat, 29 Sep 2007, Phlip wrote:
>>
>>> SpringFlowers AutumnMoon wrote:
>>>
>>>> (and in Ruby, we call a method by "pass by value, the value
>>>> being the
>>>> reference (pointer) to an object). and when the method returns
>>>> something, it returns a value, which is the reference to an
>>>> object.) It
>>>> is very consistent all the way. In Ruby, we don't have the "alias
>>>> reference", right?
>>>
>>> In Ruby, types that can (generally) fit into 32 bits are "immediate
>>> types". These Fixnums and Floats are pass-by-value. All other
>>> types -
>>> from String up - are pass by reference:
>>
>> I'd describe it more as SpringFlowers did: pass by value, where the
>> value happens to be a reference.
> Completely agruee with that. IIRC this discussion has been there quite
> a while ago and general agreement was not reached on it.

That's the way it is described in Java as well, Java is pass-by-
value, you pass references by value. C is pass-by-value as well, when
you modify something through a pointer you are passing a pointer by
value.

Perl on the other hand is pass-by-reference:

$ perl -wle '$a = 0; sub { $_[0] = 1 }->($a); print $a'
1

-- fxn


Robert Dober

9/29/2007 10:59:00 AM

0

On 9/29/07, Xavier Noria <fxn@hashref.com> wrote:
> On Sep 29, 2007, at 11:37 AM, Robert Dober wrote:
>
> > On 9/29/07, David A. Black <dblack@rubypal.com> wrote:
> >> Hi --
> >>
> >> On Sat, 29 Sep 2007, Phlip wrote:
> >>
> >>> SpringFlowers AutumnMoon wrote:
> >>>
> >>>> (and in Ruby, we call a method by "pass by value, the value
> >>>> being the
> >>>> reference (pointer) to an object). and when the method returns
> >>>> something, it returns a value, which is the reference to an
> >>>> object.) It
> >>>> is very consistent all the way. In Ruby, we don't have the "alias
> >>>> reference", right?
> >>>
> >>> In Ruby, types that can (generally) fit into 32 bits are "immediate
> >>> types". These Fixnums and Floats are pass-by-value. All other
> >>> types -
> >>> from String up - are pass by reference:
> >>
> >> I'd describe it more as SpringFlowers did: pass by value, where the
> >> value happens to be a reference.
> > Completely agruee with that. IIRC this discussion has been there quite
> > a while ago and general agreement was not reached on it.
>
> That's the way it is described in Java as well, Java is pass-by-
> value, you pass references by value. C is pass-by-value as well, when
> you modify something through a pointer you are passing a pointer by
> value.
>
> Perl on the other hand is pass-by-reference:
>
> $ perl -wle '$a = 0; sub { $_[0] = 1 }->($a); print $a'
hmm I am not sure about it,

perl -e '@x=qw{a};print $x[0]; sub{ @_ = qw{b}}->(@x); print $x[0]'

I guess the best thing one could say is

perl simulates pass by reference by passing one array by value.

Of course if one makes abstraction of @_...

Maybe not the best place to discuss this :(
Robert

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

David A. Black

9/29/2007 11:10:00 AM

0

Robert Dober

9/29/2007 12:19:00 PM

0

On 9/29/07, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
> On Sat, 29 Sep 2007, Robert Dober wrote:
>
> > On 9/29/07, David A. Black <dblack@rubypal.com> wrote:
> >> Hi --
> >>
> >> On Sat, 29 Sep 2007, Phlip wrote:
> >>
> >>> SpringFlowers AutumnMoon wrote:
> >>>
> >>>> (and in Ruby, we call a method by "pass by value, the value being the
> >>>> reference (pointer) to an object). and when the method returns
> >>>> something, it returns a value, which is the reference to an object.) It
> >>>> is very consistent all the way. In Ruby, we don't have the "alias
> >>>> reference", right?
> >>>
> >>> In Ruby, types that can (generally) fit into 32 bits are "immediate
> >>> types". These Fixnums and Floats are pass-by-value. All other types -
> >>> from String up - are pass by reference:
> >>
> >> I'd describe it more as SpringFlowers did: pass by value, where the
> >> value happens to be a reference.
> > Completely agruee with that. IIRC this discussion has been there quite
> > a while ago and general agreement was not reached on it.
>
> I don't think there's much ambiguity; when you do:
>
> s = "string"
>
> you're binding s to a reference to the object on the right.
>
> Are you thinking of the discussion about whether or not it's
> important/useful to note the distinction between references and
> immediate values in variables?
No rather this one
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
seems I was completely confused that day between reference and value
:(, who knows why?
Cheers
Robert
--
what do I think about Ruby?
http://ruby-smalltalk.blo...

SpringFlowers AutumnMoon

9/29/2007 2:05:00 PM

0

7stud -- wrote:

> A C++ reference, which is a different type than a pointer in C++, is
> actually implemented as a pointer behind the scenes. However, C++
> references allow you to use a different syntax that doesn't require
> dereferencing:
>
> int x = 10;
> int& r = x; //r becomes a pointer to the same address as x
> r = 5;
>
> cout<<x<<" "<<r<<endl; //5 5


I am starting to see what pointer and reference are and how they relate
to each other.

in the C era, a pointer *is* a reference. that's why when we have

int a = 10;
int *pi = &a;

and you can "dereference it":

*pi = 20;

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
i = 20; // now both a and i are 20

so this type of reference is an implicit pointer... it points to a, but
you don't use the way in C (int *pi = &a) And when you use (i = 20),
it does the dereference silently. (*pi = 20;)

so a reference is new: a pointer but "looks like not a pointer".

come to think about it, in Java and Ruby, they are like that too.

a = Car.new

a doesn't look like a pointer, but it is actually a pointer.

we don't dereference it to get to the attributes like (*a).value = 10
or a->value = 10 but just use a.value = 10

So from this point on, a reference and a pointer are not the same... a
reference is a pointer "that doesn't look like a pointer."

they both points to something. but the syntax (or grammar) of usage
doesn't look like it is a pointer in the C era. a reference is an
"automatically dereferenced" pointer, shall we say? or an "implicit"
pointer, or "silent" pointer.
$a = 10;
$b =& $a; # now $b implicitly points to $a
$b = 20; # now $b implicitly points to $a, which is 20

$a = new Foo("hello"); # $a implicitly points to a Foo object
# the Foo object is 100 bytes,
# but $a is just 4 bytes

$b =& $a; # $b implicitly points to $a.
# $b is a pointer to pointer
# $b points to a four byte pointer, which is $a

$b = new Foo("ok"); # dereference $b and sets its content to
# a new pointer to another object Foo("ok")
# that is, $a points to Foo("ok") now
# $b still points to $a, which points to Foo("ok")

So now, when you print $b and $a, they are both Foo("ok")

So now gets back to Ruby, do we have something like the above

a = 10
b = ____lineA1_____
b = ____lineA2_____

and now b implicitly points to a, which implicitly points to something
else, not 10 any more.

similarly

a = Dog.new
b = ____lineB1_____
b = ____lineB2_____

and now b implicitly points to a, which implicitly points to something
else, not the original Dog.new object any more.

Do we have that in Ruby?



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