Clone a customized string : clone « Class « Ruby






Clone a customized string


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"
s3.string                                      # => "spring"

 








Related examples in the same category

1.Making a Copy of an Object
2.Create a class dynamically