官术网_书友最值得收藏!

Adding a bang

Now I'm going to show you the difference between gsub and gsub!.

Going back to the program, if you print the str variable out after running through the substitution process, the output will still be "The quick brown fox jumped over the quick dog" because the gsub method did not change the variable. On the other hand, if you use gsub!, and then print the value of str, you can see that the str variable now has the "The slow brown fox jumped over the slow dog" value:

This gsub! call can be particularly useful when you want to permanently change the value of the variable. However, you need to be careful, especially when working with legacy systems, because you don't want to accidentally make a permanent change to someone else's variable in case they are expecting a specific value.

主站蜘蛛池模板: 延长县| 五大连池市| 余庆县| 西乡县| 湘西| 淮南市| 基隆市| 桑日县| 新兴县| 大姚县| 白银市| 任丘市| 阜康市| 灵山县| 平原县| 佛教| 周至县| 沛县| 怀来县| 阳江市| 孙吴县| 筠连县| 丰镇市| 乐昌市| 哈密市| 福建省| 沧州市| 凤阳县| 临洮县| 宁海县| 新泰市| 白城市| 师宗县| 卓尼县| 高阳县| 广西| 新绛县| 望江县| 寿宁县| 浦县| 株洲市|