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

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.

主站蜘蛛池模板: 青冈县| 绥阳县| 乐山市| 冕宁县| 遂昌县| 荥经县| 滕州市| 离岛区| 开平市| 咸丰县| 上犹县| 商南县| 湾仔区| 霍林郭勒市| 保定市| 方山县| 石屏县| 柞水县| 深泽县| 黄骅市| 中超| 太和县| 新源县| 伊金霍洛旗| 山阴县| 江阴市| 宣城市| 固阳县| 清水县| 九寨沟县| 永丰县| 乐都县| 称多县| 英山县| 东阳市| 澳门| 安仁县| 蕉岭县| 科尔| 林芝县| 株洲市|