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-forum.com/.
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-forum.com/.
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 = 50puts 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.rubypal.com for details and updates!
a = 10
b = a
a = 50puts 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 Apr 29, 2008, at 13:02, Marcin Tyman wrote:
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
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 = 50puts b # -> puts 10
Why?? Since everything is a reference in Ruby I expected "puts 50".
--
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: 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 = 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-forum.com/.
Marcin Tyman said...
I was quite a bit confused when write following code:
a = 10
b = a
a = 50puts 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
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 = 50puts 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.rubypal.com for details and updates!
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 = 50puts 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.rubypal.com for details and updates!
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>
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.
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
David A. Black wrote:
Hi --
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?
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.rubypal.com for details and updates!