A
Aytek Yüksel
Hi,
I'm new to ruby. Trying to understand how variables hold references to
objects.
I wrote some simple example for this:
##
# Trying to observ person1 and person2 are aliases.
# (they references the same object)
person1 = "Tim"
person2 = person1
person2[0] = "s" # This changes both
puts person1
puts person2
# Produces:
#
# Tim
# sim
# Second part
person3 = "Tim"
person4 = person3
person4 = "s"
puts person3
puts person4
# Produces:
#
# Tim
# s
I expect that the second part will print:
s # not Tim
s
What's the reason that when I assign something to person3, person4
doesn't change although they are aliases (or aren't they?)
Thanks,
Aytek
I'm new to ruby. Trying to understand how variables hold references to
objects.
I wrote some simple example for this:
##
# Trying to observ person1 and person2 are aliases.
# (they references the same object)
person1 = "Tim"
person2 = person1
person2[0] = "s" # This changes both
puts person1
puts person2
# Produces:
#
# Tim
# sim
# Second part
person3 = "Tim"
person4 = person3
person4 = "s"
puts person3
puts person4
# Produces:
#
# Tim
# s
I expect that the second part will print:
s # not Tim
s
What's the reason that when I assign something to person3, person4
doesn't change although they are aliases (or aren't they?)
Thanks,
Aytek