Ruby/Class/clone
Материал из Wiki.crossplatform.ru
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"
Create a class dynamically
material = "cotton" class << material def definition puts "this is a test." end end puts material.definition "cotton".definition # NoMethodError: undefined method "definition" for "cotton":String material.clone.definition material.dup.definition # NoMethodError: undefined method "definition" for "cotton":String
Making a Copy of an Object
s1 = "foo" # => "foo" s2 = s1.clone # => "foo" s1[0] = "b" [s1, s2] # => ["boo", "foo"]