“!”的目的是什么?和“?”在方法名称的末尾?

发布于 2024-12-01 00:06:11 字数 172 浏览 4 评论 0原文

有时我会看到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月依秋水 2024-12-08 00:06:11

它只是为了可读性而“糖衣”,但它们确实有共同的含义:

  • ! 结尾的方法执行一些永久性或潜在危险的更改;例如:
    • Enumerable#sort 返回对象的排序版本,而 Enumerable#sort! 则对其进行就地排序。
    • 在 Rails 中,如果保存失败,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:

  • Methods ending in ! perform some permanent or potentially dangerous change; for example:
    • Enumerable#sort returns a sorted version of the object while Enumerable#sort! sorts it in place.
    • In Rails, ActiveRecord::Base#save returns false if saving failed, while ActiveRecord::Base#save! raises an exception.
    • Kernel::exit causes a script to exit, while Kernel::exit! does so immediately, bypassing any exit handlers.
  • Methods ending in ? return a boolean, which makes the code flow even more intuitively like a sentence — if number.zero? reads like "if the number is zero", but if number.zero just looks weird.

In your example, name.reverse evaluates to a reversed string, but only after the name.reverse! line does the name variable actually contain the reversed name. name.is_binary_data? looks like "is name binary data?".

神回复 2024-12-08 00:06:11

问号表示该方法返回布尔值。已经在这里回答:

问号运算符在 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?

橪书 2024-12-08 00:06:11

在 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.

风流物 2024-12-08 00:06:11

与我认为的大多数编程语言相比......

Ruby,方法允许以问号或感叹号结尾。

按照惯例,回答问题的方法(即 Array#empty? 如果接收者为空则返回 true)以问号结尾。

按照惯例,潜在的“危险”方法(即修改自身或参数、退出!等的方法)以感叹号结尾。

来自: http://www.ruby-lang.org/ en/documentation/ruby-from-other-linguals/,部分有趣的方法名称

In contrast to the – I suppose – majority of programming languages ...

Ruby, methods are allowed to end with question marks or exclamation marks.

By convention, methods that answer questions (i.e. Array#empty? returns true if the receiver is empty) end in question marks.

Potentially “dangerous” methods (ie methods that modify self or the arguments, exit! etc.) by convention end with exclamation marks.

From: http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/, Section Funny method names

美人迟暮 2024-12-08 00:06:11

请注意,情况并非总是如此。以 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文