 
 This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Making a Copy of an Object | ||
|---|---|---|
| Code | Expected | Actual | 
| s1 = 'foo' | "foo" | "foo" | 
| s2 = s1.clone | "foo" | "foo" | 
| s1[0] = 'b' [s1, s2] | ["boo", "foo"] | ["boo", "foo"] | 
| material = 'cotton'
class << material
  def definition
     puts 'The better half of velour.'
  end
end
material.definition | The better half of velour. | The better half of velour. | 
| 'cotton'.definition | NoMethodError: undefined method `definition' for "cotton":String ... | NoMethodError: undefined method `definition' for "cotton":String from (irb):12 | 
| material.clone.definition | The better half of velour. | The better half of velour. | 
| material.dup.definition | NoMethodError: undefined method `definition' for "cotton":String ... | NoMethodError: undefined method `definition' for "cotton":String from (irb):14 | 
| class StringHolder 
  attr_reader :string
  def initialize(string)
    @string = string
  end
end
s1 = StringHolder.new('string')
s2 = s1.dup
s3 = s1.clone
s1.string[1] = 'p'
s2.string | "spring" | "spring" | 
| s3.string | "spring" | "spring" | 
| class Object
  def deep_copy
    Marshal.load(Marshal.dump(self))
  end
end
s1 = StringHolder.new('string')
s2 = s1.deep_copy
s1.string[1] = 'p'
s1.string | "spring" | "spring" | 
| s2.string | "string" | "string" | 
| class << s1
  def definition
     puts "We hold strings so you don't have to."
  end  
end
s1.deep_copy | TypeError: singleton can't be dumped ... | TypeError: singleton can't be dumped from (irb):29:in `dump' from (irb):29:in `deep_copy' from (irb):42 | 
| class StringHolder
  def initialize_copy(from)
   @string = from.string.dup
  end
end
s1 = StringHolder.new('string')
s2 = s1.dup
s3 = s1.clone
s1.string[1] = "p"
s2.string | "string" | "string" | 
| s3.string | "string" | "string" |