“!”的目的是什么?和“?”在方法名称的末尾?
有时我会看到 Ruby 中的方法带有“?”和 ”!”在他们的最后,例如:
name = "sample_string"
name.reverse
name.reverse!
name.is_binary_data?
我想知道他们的目的是什么?它们只是语法糖衣吗?
Sometimes I see methods in Ruby that have "?" and "!" at the end of them, e.g:
name = "sample_string"
name.reverse
name.reverse!
name.is_binary_data?
I was wondering what their purpose is? Are they just syntax sugarcoating?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它只是为了可读性而“糖衣”,但它们确实有共同的含义:
!
结尾的方法执行一些永久性或潜在危险的更改;例如:Enumerable#sort
返回对象的排序版本,而Enumerable#sort!
则对其进行就地排序。ActiveRecord::Base#save
返回 false,而ActiveRecord::Base#save!
会引发异常。Kernel::exit
导致脚本退出,而Kernel::exit!
会立即执行此操作,绕过任何退出处理程序。?
结尾的方法返回一个布尔值,这使得代码流程更加直观,就像一个句子 -if number.zero?
读起来像“if数字为零”,但是if number.zero
看起来很奇怪。在您的示例中,
name.reverse
计算结果为反转字符串,但只有在name.reverse!
行之后,name
变量才真正包含颠倒的名称。name.is_binary_data?
看起来像“name
是二进制数据吗?”。It's "just sugarcoating" for readability, but they do have common meanings:
!
perform some permanent or potentially dangerous change; for example:Enumerable#sort
returns a sorted version of the object whileEnumerable#sort!
sorts it in place.ActiveRecord::Base#save
returns false if saving failed, whileActiveRecord::Base#save!
raises an exception.Kernel::exit
causes a script to exit, whileKernel::exit!
does so immediately, bypassing any exit handlers.?
return a boolean, which makes the code flow even more intuitively like a sentence —if number.zero?
reads like "if the number is zero", butif number.zero
just looks weird.In your example,
name.reverse
evaluates to a reversed string, but only after thename.reverse!
line does thename
variable actually contain the reversed name.name.is_binary_data?
looks like "isname
binary data?".问号表示该方法返回布尔值。已经在这里回答:
问号运算符在 Ruby 中意味着什么?
感叹号表示该方法作用于物体本身。已经在这里回答:
为什么在 Ruby 方法中使用感叹号?
Question mark indicates that the method returns boolean. Already answered here:
What does the question mark operator mean in Ruby?
The bang indicates that the method acts on the object itself. Already answered here:
Why are exclamation marks used in Ruby methods?
在 Ruby 中,
?
表示该方法将返回一个布尔值,而!
会修改调用它的对象。它们的作用是提高查看代码时的可读性。In Ruby the
?
means that the method is going to return a boolean and the!
modifies the object it was called on. They are there to improve readability when looking at the code.与我认为的大多数编程语言相比......
来自: http://www.ruby-lang.org/ en/documentation/ruby-from-other-linguals/,部分有趣的方法名称
In contrast to the – I suppose – majority of programming languages ...
From: http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/, Section Funny method names
请注意,情况并非总是如此。以 Ruby Array#concat http:// /docs.ruby-lang.org/en/2.0.0/Array.html#method-i-concat。
可能会严重烧毁的地方是类似
MyActiveRecordModel.column_names.concat([url])
的东西。稍后与 MyActiveRecordModel 相关的调用将尝试查找 MyActiveRecordModel 的“url”列并抛出。相反,您必须在进行连接之前克隆它。幸运的是,我的测试套件发现了这个,但是..请注意!
Beware, this isn't always the case. Take for example, Ruby Array#concat http://docs.ruby-lang.org/en/2.0.0/Array.html#method-i-concat.
Where you can get burnt badly is something like
MyActiveRecordModel.column_names.concat([url])
. Later calls related to MyActiveRecordModel will try to look for a column of 'url' for MyActiveRecordModel and throw.Instead you must clone it before doing the concat. Fortunately my test suite caught this one, but.. heads up!