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

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.

主站蜘蛛池模板: 门源| 馆陶县| 教育| 读书| 江津市| 蚌埠市| 兰州市| 赤峰市| 霍城县| 南川市| 洛川县| 德清县| 青川县| 娱乐| 通化县| 惠安县| 胶州市| 田阳县| 竹北市| 友谊县| 长乐市| 达拉特旗| 邛崃市| 安新县| 浮山县| 霍林郭勒市| 洪泽县| 淳安县| 卢龙县| 百色市| 武陟县| 阿城市| 兴安盟| 平顺县| 迁西县| 什邡市| 建昌县| 白水县| 龙游县| 嘉义市| 偃师市|