19 lines
330 B
Text
19 lines
330 B
Text
# Substitute in copy
|
|
|
|
str2 = str.sub(/ a /, " another ")
|
|
|
|
p str # original unchanged
|
|
p str2 # prints "I am another string"
|
|
|
|
# Substitute in place
|
|
|
|
str.sub!(/ a /, " another ")
|
|
|
|
p str # prints "I am another string"
|
|
|
|
# Substitute with a block
|
|
|
|
str.sub! /a/
|
|
{ match | match.upcase }
|
|
|
|
p str # prints "I Am Another string"
|