References in Ruby

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.

Hi –

On Tue, 29 Apr 2008, Marcin T. wrote:

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

On Apr 29, 2008, at 13:02, Marcin T. 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.

On Tue, Apr 29, 2008 at 7:02 AM, Marcin T. [email protected]
wrote:

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.denhaven2.com/articles/2006/09/13/on-variables-values-and-objects


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Tue, Apr 29, 2008 at 7:37 AM, David A. Black [email protected]
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.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

[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 like that in Ruby when you are assigning to variables. Think
about it more like a pointer assignment in C.

char c = ‘c’, d = ‘d’;
char* p = &c;

p = &d;

The last operation does not change the content of variable c, simply
changes the pointer value in p to “refer” to d.

Gennady.


From: [email protected] [[email protected]] On Behalf Of
Marcin T. [[email protected]]
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 = 10
b = a
a = 50

puts b # -> puts 10

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

Thanks for answer.

Hi –

On Tue, 29 Apr 2008, Rick DeNatale wrote:

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

Marcin T. 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:

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 :slight_smile:

David

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 :slight_smile:

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?

Hi –

On Fri, 2 May 2008, Stephen Cox wrote:

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