# Ruby 中识别方法的约定的基本原理/历史是什么?
例如,我总是看到称为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
差异表明您如何访问这些方法。
类方法使用
::
分隔符来指示消息可以发送到类/模块对象,而实例方法使用#
分隔符,指示消息可以发送到实例对象。我将选择
Complex
类(在 Ruby 1.9 中)来演示其中的差异。您同时拥有Complex::rect
和Complex#rect
。这些方法具有不同的数量,并且它们服务于完全不同的目的。Complex::rect
接受一个实数和一个虚数参数,返回一个Complex
的新实例,而Complex#rect
返回一个实数数组和实例的虚部。我认为他们不使用
.
作为所有内容的分隔符的原因是,该方法属于类还是实例会不明确。现在我已经习惯了 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 bothComplex::rect
andComplex#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 ofComplex
, whileComplex#rect
returns an array of the real and imaginary components of the instance.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 orObject#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.是的,这就是它的来源。当您使用
#
时,它会自动超链接您的方法,因此文档中对其他方法的引用开始以#
符号作为前缀。请参阅此处:然而,您实际上不能以这种方式调用方法。但这并不奇怪。毕竟,尽管
是有效的文档标记,但它在 C# 中是无效语句。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: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.