! 是什么意思? Ruby 方法名称后面的含义是什么?
Possible Duplicate:
Why are exclamation marks used in Ruby methods?
While looking over some Ruby code I came across this method:
def SomeMethod!
// ...
end
What does the ! following the method name do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这只是一种将方法标记为“比其他方法更危险”的约定。这可能意味着它具有影响其参数或类属性的副作用。这只是提醒要小心。
I believe it's just a convention to mark a method as 'more dangerous than others." This could mean that it has a side effect that affects its parameters or maybe its class attributes. It's just a reminder to be careful.
按照惯例,这意味着该方法会改变类的状态。
By convention it means that the method mutates the state of the class.
方法名称末尾的感叹号意味着此类方法以比不带
!
的相同方法的版本更危险的方式执行操作。请注意,如果没有这样的其他方法,则永远不应该使用
!
。您可以将!
理解为一种在视觉上区分两个等效方法的方法。示例:
Array#flatten
Array#flatten!
Kernel#exit!
内核#退出
The exclamation mark at the end of a method's name means that such a method performs an operation in a more dangerous way than the version of the same method without the
!
.Notice that you should never ever use
!
if there is not such other method. You can understand!
as a means to visually differentiate between two otherwise equivalent methods.Example:
Array#flatten
Array#flatten!
Kernel#exit!
Kernel#exit