Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > a different type of reference (shocked)

Reply
Thread Tools

a different type of reference (shocked)

 
 
SpringFlowers AutumnMoon
Guest
Posts: n/a
 
      09-28-2007
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-forum.com/.

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      09-28-2007
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/9780596510657/
^ assert_xpath
http://tinyurl.com/yrc77g <-- assert_latest Model

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

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      09-29-2007
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-forum.com/.

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

s = "string"
do_something(s)

you are passing s by its value, which is a reference to the string
object.


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
 
Robert Dober
Guest
Posts: n/a
 
      09-29-2007
On 9/29/07, David A. Black <> 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.blogspot.com/

 
Reply With Quote
 
Xavier Noria
Guest
Posts: n/a
 
      09-29-2007
On Sep 29, 2007, at 11:37 AM, Robert Dober wrote:

> On 9/29/07, David A. Black <> 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


 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      09-29-2007
On 9/29/07, Xavier Noria <> wrote:
> On Sep 29, 2007, at 11:37 AM, Robert Dober wrote:
>
> > On 9/29/07, David A. Black <> 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.blogspot.com/

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      09-29-2007
Hi --

On Sat, 29 Sep 2007, Robert Dober wrote:

> On 9/29/07, David A. Black <> 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?


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
 
Robert Dober
Guest
Posts: n/a
 
      09-29-2007
On 9/29/07, David A. Black <> wrote:
> Hi --
>
> On Sat, 29 Sep 2007, Robert Dober wrote:
>
> > On 9/29/07, David A. Black <> 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...by-talk/236400
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.blogspot.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
Re: C++ objects are value type or reference type bhadorn C++ 0 10-30-2009 07:34 PM
Is "reference" a (different?) type? Neelesh Bodas C++ 4 07-15-2007 10:52 AM
Different tuples to one container? (One type of a pointer to point to different kinds of tuples?) fff_afafaf@yahoo.com C++ 5 10-05-2006 11:17 PM
Value type or reference type Sam Sungshik Kong Ruby 1 06-02-2004 09:48 PM
Different transforms for different children of same type Ravi XML 4 11-10-2003 08:22 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57