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

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.

主站蜘蛛池模板: 渝中区| 呈贡县| 太仓市| 永新县| 南投县| 密山市| 永修县| 高安市| 虞城县| 诸城市| 山东| 巴林右旗| 多伦县| 云梦县| 大足县| 武宣县| 连云港市| 鄂温| 浦城县| 房山区| 弥渡县| 安达市| 文昌市| 安福县| 博爱县| 彭州市| 华池县| 睢宁县| 建湖县| 长沙市| 陆丰市| 新建县| 民乐县| 静乐县| 崇信县| 内黄县| 农安县| 巴林右旗| 西乡县| 商水县| 泽普县|