RosettaCodeData/Task/Copy-a-string/Ruby/copy-a-string-2.rb

8 lines
394 B
Ruby

original = "hello".freeze # prevents further modifications
copy1 = original.dup # copies contents (without status)
copy2 = original.clone # copies contents (with status)
p copy1.frozen? #=> false
p copy1 << " world!" #=> "hello world!"
p copy2.frozen? #=> true
p copy2 << " world!" #=> can't modify frozen String (RuntimeError)