ruby - 为什么是替换而不是替换!对于 ruby 中的字符串?

标签 ruby immutability

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/

相关文章:

ruby-on-rails - 部署后缺少 gem (Ruby、Ruby on Rails、Capistrano)

ruby - 有没有其他方法可以触发 capybara ?

java - 强制不变性

ruby - ruby 正则表达式中的引号被误解为字符串的开头

ruby-on-rails - 带有自定义 header 和正文的 Ruby POST?

java - 制作不可变的 Java 对象

list - Scala 不可变列表添加 'Unit' 元素

java - 这个东西在Java中叫什么?

ruby-on-rails - 使用 Rails 4.1 在 hstore 字段中选择单个属性

java - 禁用 Java 列表中的可变性