我刚刚开始学习 Ruby,我不太了解带和不带 '!' 的几种 Ruby 方法之间的区别。在最后。有什么不同?为什么我要使用一个而不是另一个?
最佳答案
末尾带有感叹号的方法通常称为 bang-methods。 bang 方法不一定会修改其接收者,也不能保证没有感叹号的方法不会。
这一切在 this blog post 中都有很好的解释。 .引用帖子:
The ! in method names that end with ! means, “This method is dangerous”—or, more precisely, this method is the “dangerous” version of an otherwise equivalent method, with the same name minus the !. “Danger” is relative; the ! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name.
和
The ! does not mean “This method changes its receiver.” A lot of “dangerous” methods do change their receivers. But some don’t. I repeat: ! does not mean that the method changes its receiver.
关于ruby - downcase 和 downcase 的区别!在 ruby ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/709229/