J
Jonah Bloch-Johnson
Hello there Ruby-minded people!
I am writing because I am somewhat stumped by why the following
discrepency occurs; having defined the following:
class Warfare
def haves
x = [1]
y = []
y << x
x = [2] # (compare this)
y
end
def havenots
x = [1]
y = []
y << x
x[0] = 2 # (to this)
y
end
end
I get this in irb:
Why does meddling with the innards of the "x" have retroactive
repercussions, whereas redefining it outright does not?
Any insight would be greatly appreciated.
-Jonah
I am writing because I am somewhat stumped by why the following
discrepency occurs; having defined the following:
class Warfare
def haves
x = [1]
y = []
y << x
x = [2] # (compare this)
y
end
def havenots
x = [1]
y = []
y << x
x[0] = 2 # (to this)
y
end
end
I get this in irb:
=> [[2]]load 'snah.rb' => true
a = Warfare.new=> # said:a.haves => [[1]]
a.havenots
Why does meddling with the innards of the "x" have retroactive
repercussions, whereas redefining it outright does not?
Any insight would be greatly appreciated.
-Jonah