Saturday, 7 September 2013

Whats the difference between 'input = self' and 'input = self.dup'

Whats the difference between 'input = self' and 'input = self.dup'

So the problem I'm having is understanding the difference between = self
and = self dup. When I run the code below any array I put into it is
permanently changed.
class Array
def pad(min_size, value = nil)
input = self
counter = min_size.to_i - self.length.to_i
counter.times do
input.push(value)
end
input
end
end
But then I noticed if I put input = self.dup it would not permanently
change my array. Can someone explain why? Thanks!
class Array
def pad(min_size, value = nil)
input = self.dup
counter = min_size.to_i - self.length.to_i
counter.times do
input.push(value)
end
input
end
end

No comments:

Post a Comment