[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

References in Ruby

Marcin Tyman

4/29/2008 11:03:00 AM

Hi guys,

I was quite a bit confused when write following code:

a = 10
b = a
a = 50

puts b # -> puts 10

Why?? Since everything is a reference in Ruby I expected "puts 50".


Thanks for answer.
--
Posted via http://www.ruby-....

10 Answers

David A. Black

4/29/2008 11:18:00 AM

0

Hi --

On Tue, 29 Apr 2008, Marcin Tyman wrote:

> Hi guys,
>
> I was quite a bit confused when write following code:
>
> a = 10
> b = a
> a = 50
>
> puts b # -> puts 10
>
> Why?? Since everything is a reference in Ruby I expected "puts 50".

When you assign to a variable, you are reusing the identifer (a, in
this case). Any previous binding between the identifier and a value is
discarded.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Mikael Høilund

4/29/2008 11:22:00 AM

0


On Apr 29, 2008, at 13:02, Marcin Tyman wrote:
> a = 10
> b = a
> a = 50
>
> puts b # -> puts 10
>
> Why?? Since everything is a reference in Ruby I expected "puts 50".

That's because you're not changing 50, you're changing a. At the third
line, you're setting a to be a different object than b. Compare to:

>> a = "Hello"
=> "Hello"
>> b = a
=> "Hello"
>> a.upcase!
=> "HELLO"
>> b
=> "HELLO"

Play around with Object#object_id for a better idea of how this works.

Rick DeNatale

4/29/2008 11:31:00 AM

0

On Tue, Apr 29, 2008 at 7:02 AM, Marcin Tyman <m.tyman@interia.pl> wrote:
> Hi guys,
>
> I was quite a bit confused when write following code:
>
> a = 10
> b = a
> a = 50
>
> puts b # -> puts 10
>
> Why?? Since everything is a reference in Ruby I expected "puts 50".

Well not everything IS a reference in Ruby. I think you are suffering
from a variation of the common nuby problem of misunderstanding the
distinction between variables and objects.

Objects have identity and state, sometimes that state is changeable
(only by sending the object a message).

Variables refer to objects. Variables DON"T refer to other objects.
A variable gets bound to a particular object by assignment, only that
variable's binding gets change by the assignment. Let's step through
your little program

code variables objects
a = 10 a ------------------> 10
^
b = a b----------------------+

This is the situation after the first two lines. But after the third we have:

a = 50 a -------------------> 50
b--------------------> 10

This article might help:
http://talklikeaduck.denh...articles/2006/09/13/on-variables-values-a...

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

David A. Black

4/29/2008 11:37:00 AM

0

Hi --

On Tue, 29 Apr 2008, Rick DeNatale wrote:

> On Tue, Apr 29, 2008 at 7:02 AM, Marcin Tyman <m.tyman@interia.pl> wrote:
>> Hi guys,
>>
>> I was quite a bit confused when write following code:
>>
>> a = 10
>> b = a
>> a = 50
>>
>> puts b # -> puts 10
>>
>> Why?? Since everything is a reference in Ruby I expected "puts 50".
>
> Well not everything IS a reference in Ruby. I think you are suffering
> from a variation of the common nuby problem of misunderstanding the
> distinction between variables and objects.
>
> Objects have identity and state, sometimes that state is changeable
> (only by sending the object a message).
>
> Variables refer to objects. Variables DON"T refer to other objects.

s/objects\.$/variables./ I think.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Rick DeNatale

4/29/2008 11:43:00 AM

0

On Tue, Apr 29, 2008 at 7:37 AM, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
>
> On Tue, 29 Apr 2008, Rick DeNatale wrote:

> > Variables refer to objects. Variables DON"T refer to other objects.
> >
>
> s/objects\.$/variables./ I think.

Yes, for the regex challenged that should read,

Variables refer to objects, Variables DON'T refer to other variables.

Proving once again that two cups of coffee don't provide enough
caffeine at this hour of the morning. <G>

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Gennady Bystritsky

4/29/2008 2:17:00 PM

0

[Sorry for the top posting, webmail not quoting properly]

Apparently you are thinking about references in C++ sense -- once assigned,=
subsequent assignment operations change the underlying object. It is NOT l=
ike that in Ruby when you are assigning to variables. Think about it more =
like a pointer assignment in C.

char c =3D 'c', d =3D 'd';
char* p =3D &c;

p =3D &d;

The last operation does not change the content of variable c, simply change=
s the pointer value in p to "refer" to d.

Gennady.

________________________________________
From: list-bounce@example.com [list-bounce@example.com] On Behalf Of Marcin=
Tyman [m.tyman@interia.pl]
Sent: Tuesday, April 29, 2008 4:02 AM
To: ruby-talk ML
Subject: References in Ruby

Hi guys,

I was quite a bit confused when write following code:

a =3D 10
b =3D a
a =3D 50

puts b # -> puts 10

Why?? Since everything is a reference in Ruby I expected "puts 50".


Thanks for answer.
--
Posted via http://www.ruby-....


marc

5/1/2008 1:24:00 PM

0

Marcin Tyman said...

> I was quite a bit confused when write following code:
>
> a = 10
> b = a
> a = 50
>
> puts b # -> puts 10
>
> Why?? Since everything is a reference in Ruby I expected "puts 50".

The way I think of it is that 10 and 50 are objects. So,

1. a = 10 # a -> the object 10
2. b = a # b -> the object 10
3. a = 50 # a -> the object 50
4. puts b # -> 10

And Mikael's explanation helps too, imo:

>> a = "Hello" => "Hello"
>> b = a => "Hello"
>> a.upcase! => "HELLO"
>> b => "HELLO"

--
Cheers,
Marc


David A. Black

5/1/2008 2:47:00 PM

0

Hi --

On Thu, 1 May 2008, marc wrote:

> Marcin Tyman said...
>
>> I was quite a bit confused when write following code:
>>
>> a = 10
>> b = a
>> a = 50
>>
>> puts b # -> puts 10
>>
>> Why?? Since everything is a reference in Ruby I expected "puts 50".
>
> The way I think of it is that 10 and 50 are objects.

And indeed they are :-)


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Stephen Cox

5/1/2008 6:07:00 PM

0

David A. Black wrote:
> Hi --
>
> On Thu, 1 May 2008, marc wrote:
>
>>> Why?? Since everything is a reference in Ruby I expected "puts 50".
>>
>> The way I think of it is that 10 and 50 are objects.
>
> And indeed they are :-)
>
>
> David

Wait, I'm a noob. And I see it this way:

a = 10
b = a # b is NOW 10
a = 50 # what's that got to do with B? B has already been assigned.

I know I'm came to the same answer, but am I thinking this through the
right way?


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

David A. Black

5/1/2008 6:11:00 PM

0

Hi --

On Fri, 2 May 2008, Stephen Cox wrote:

> David A. Black wrote:
>> Hi --
>>
>> On Thu, 1 May 2008, marc wrote:
>>
>>>> Why?? Since everything is a reference in Ruby I expected "puts 50".
>>>
>>> The way I think of it is that 10 and 50 are objects.
>>
>> And indeed they are :-)
>>
>>
>> David
>
> Wait, I'm a noob. And I see it this way:
>
> a = 10
> b = a # b is NOW 10
> a = 50 # what's that got to do with B? B has already been assigned.
>
> I know I'm came to the same answer, but am I thinking this through the
> right way?

Yes, I think you are. 10 and 50 are objects (of class Fixnum). The
changing of the binding of a from 10 to 50 has no effect on the
binding of b to 10.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!