Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Copy-a-string/Ruby/copy-a-string-1.rb
Normal file
9
Task/Copy-a-string/Ruby/copy-a-string-1.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
original = "hello"
|
||||
reference = original # copies reference
|
||||
copy1 = original.dup # instance of original.class
|
||||
copy2 = String.new(original) # instance of String
|
||||
|
||||
original << " world!" # append
|
||||
p reference #=> "hello world!"
|
||||
p copy1 #=> "hello"
|
||||
p copy2 #=> "hello"
|
||||
7
Task/Copy-a-string/Ruby/copy-a-string-2.rb
Normal file
7
Task/Copy-a-string/Ruby/copy-a-string-2.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue