# Ruby 中识别方法的约定的基本原理/历史是什么?

发布于 2024-09-07 07:12:59 字数 315 浏览 11 评论 0原文

例如,我总是看到称为 String#split 的方法,但从未见过 String.split,这似乎更符合逻辑。或者甚至可能是 String::split,因为您可以认为 #split 位于 String 的命名空间中。当类被假定/隐含时(#split),我什至单独看到了该方法。

我知道这是 ri 中识别方法的方式。哪一个先出现?

例如,这是为了区分方法和字段吗? 我还听说这有助于区分实例方法和类方法。但这是从哪里开始的呢?

For example, I've always seen methods referred to as String#split, but never String.split, which seems slightly more logical. Or maybe even String::split, because you could consider #split to be in the namespace of String. I've even seen the method alone, when the class is assumed/implied (#split).

I understand that this is the way methods are identified in ri. Which came first?

Is this to differentiate, for example, methods from fields?
I've also heard that this helps differentiates instance methods from class methods. But where did this start?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

生来就爱笑 2024-09-14 07:12:59

差异表明您如何访问这些方法。

类方法使用::分隔符来指示消息可以发送到类/模块对象,而实例方法使用# 分隔符,指示消息可以发送到实例对象。

我将选择 Complex 类(在 Ruby 1.9 中)来演示其中的差异。您同时拥有 Complex::rectComplex#rect。这些方法具有不同的数量,并且它们服务于完全不同的目的。 Complex::rect 接受一个实数和一个虚数参数,返回一个 Complex 的新实例,而 Complex#rect 返回一个实数数组和实例的虚部。

ruby-1.9.1-p378 > x = Complex.rect(1,5)
 => (1+5i) 
ruby-1.9.1-p378 > x.rect
 => [1, 5] 
ruby-1.9.1-p378 > x.rect(2, 4) # what would this even do?
ArgumentError: wrong number of arguments(2 for 0)
    from (irb):4:in `rect'
    from (irb):4
    from /Users/mr/.rvm/rubies/ruby-1.9.1-p378/bin/irb:17:in `<main>'

我认为他们不使用 . 作为所有内容的分隔符的原因是,该方法属于类还是实例会不明确。现在我已经习惯了 Ruby 这样做,老实说,我实际上认为它是其他语言约定的一个缺点。

此外,这在某种程度上是一个与字段完全无关的主题,因为正确地说,您可以发送的所有消息都是消息,即使它看起来像一个可公开访问的字段。当然,与字段最接近的是属性或实例变量,它们始终以 @ 为前缀,并且不能从实例外部直接访问,除非您使用继承或Object#instance_variable_get/_set

至于具体为什么选择::#:: 对我来说很有意义,因为它通常分隔命名空间,但 # 可能只是一个未在其他命名法中使用的符号,并且可以明确地被识别为实例 -方法分隔符。

The difference indicates how you access the methods.

Class methods use the :: separator to indicate that message can be sent to the class/module object, while instance methods use the # separator to indicate that the message can be sent to an instance object.

I'm going to pick the Complex class (in Ruby 1.9) to demonstrate the difference. You have both Complex::rect and Complex#rect. These methods have different arity and they serve entirely different purposes. Complex::rect takes a real and an imaginary argument, returning a new instance of Complex, while Complex#rect returns an array of the real and imaginary components of the instance.

ruby-1.9.1-p378 > x = Complex.rect(1,5)
 => (1+5i) 
ruby-1.9.1-p378 > x.rect
 => [1, 5] 
ruby-1.9.1-p378 > x.rect(2, 4) # what would this even do?
ArgumentError: wrong number of arguments(2 for 0)
    from (irb):4:in `rect'
    from (irb):4
    from /Users/mr/.rvm/rubies/ruby-1.9.1-p378/bin/irb:17:in `<main>'

I think the reason that they don't use . as the separator for everything is that it would be ambiguous whether the method belongs to a class or an instance. Now that I'm used to Ruby doing this, I actually see it as a drawback to other languages' conventions, to be honest.

Also, this is somewhat of a completely unrelated topic from fields because all messages you can send are messages, properly speaking, even if it looks like a publicly accessible field. The closest thing you have to fields are attributes or instance variables, of course, which are always prefixed with @ and are not directly accessible from outside the instance unless you are using inheritance or Object#instance_variable_get/_set.

As to specifically why they chose :: and #? :: makes sense to me because it conventionally separated namespaces, but # was probably just a symbol that wasn't used in other nomenclature and could unambiguously be recognized as an instance-method separator.

蓝戈者 2024-09-14 07:12:59

我知道这是 ri 中识别方法的方式。哪个先出现?

是的,这就是它的来源。当您使用 # 时,它会自动超链接您的方法,因此文档中对其他方法的引用开始以 # 符号作为前缀。请参阅此处

类的名称、源文件和任何包含下划线或前面带有哈希字符的方法名称都会自动从注释文本超链接到其描述。

然而,您实际上不能以这种方式调用方法。但这并不奇怪。毕竟,尽管 是有效的文档标记,但它在 C# 中是无效语句。

I understand that this is the way methods are identified in ri. Which came first?

Yes, this is where it came from. When you use #, it automatically hyperlinks your methods, so references to other methods in documentation began being prefixed by the # sign. See here:

Names of classes, source files, and any method names containing an underscore or preceded by a hash character are automatically hyperlinked from comment text to their description.

You can't actually invoke a method this way, however. But that shouldn't be surprising; after all, <cref ...> is an invalid statement in C# despite being a valid documentation tag.

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