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

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.

主站蜘蛛池模板: 和静县| 海城市| 友谊县| 绥江县| 海林市| 简阳市| 海晏县| 黑河市| 玉山县| 中方县| 奉节县| 衡水市| 阳曲县| 沿河| 吴旗县| 遵义市| 郓城县| 嘉鱼县| 阿瓦提县| 威海市| 鹤山市| 平阳县| 普兰店市| 河间市| 华池县| 江安县| 龙门县| 乌兰察布市| 舟山市| 安国市| 天门市| 彰化市| 新昌县| 忻州市| 神农架林区| 图们市| 车致| 丹江口市| 澜沧| 大同县| 永修县|