replace
改变当前字符串而不是返回一个新实例。为了与 Ruby 中的其他方法保持一致,似乎应该调用 replace!
这是错误/不一致还是我遗漏了什么?
最佳答案
来自 matz 的帖子 https://www.ruby-forum.com/topic/176830#773946
The bang (!) does not mean "destructive" nor lack of it mean non destructive either. The bang sign means "the bang version is more dangerous than its non bang counterpart; handle with care". Since Ruby has a lot of "destructive" methods, if bang signs follow your opinion, every Ruby program would be full of bangs, thus ugly.
所以最初的问题来自对 bang (!
) 含义的误解。没有爆炸,因为只有一个 replace
方法才有意义,所以没有必要将其标记为“更危险”。
关于ruby - 为什么是替换而不是替换!对于 ruby 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32609204/