B
Brian Schröder
Hello Group,
I sometimes which to make a deep copy of an object. I know I could use Marshal,
but thats slow so I want to write a routine #deep_copy. (Or should I overwrite
#dup ?)
Now the question is, how do you write those. I could use this:
class A
...
def deep_copy
result = self.dup
result.field1 = self.field1.deep_copy
...
end
end
or
class A
def initialize(field1 = 'default value', ...)
@field1 = field
...
end
def deep_copy
self.new(@field1.deep_copy)
end
end
Which allows me to use instance variables.
Is there something more elegant. What do you prefer? Am I on the right track?
Best regards,
Brian
I sometimes which to make a deep copy of an object. I know I could use Marshal,
but thats slow so I want to write a routine #deep_copy. (Or should I overwrite
#dup ?)
Now the question is, how do you write those. I could use this:
class A
...
def deep_copy
result = self.dup
result.field1 = self.field1.deep_copy
...
end
end
or
class A
def initialize(field1 = 'default value', ...)
@field1 = field
...
end
def deep_copy
self.new(@field1.deep_copy)
end
end
Which allows me to use instance variables.
Is there something more elegant. What do you prefer? Am I on the right track?
Best regards,
Brian