Ruby - 数组方法混淆

发布于 2024-08-13 20:32:06 字数 340 浏览 4 评论 0原文

我们可以像这样在顶层调用 Array 方法,

Array(something)

这对我来说很有意义,这是一个没有显式接收者的方法调用,而 self(在本例中是 main)被插入到方法调用的前面。但这不是等于:

Kernel.Array(something)

这对我来说没有意义。由于在第一种情况下,对象 main 是 Object 类,其中混入了 Kernel 模块,因此具有 Array 方法。但在第二种情况下,我们在内核模块对象本身而不是主对象上调用 Array 方法,它们不是同一件事吗?

抱歉我的英语不好。

we can call the Array method in the top level like this

Array(something)

that makes sense to me, it's a method call without explicit receiver, and self, which is main in this case, is inserted at the front of the method call. But isn't it that this is equivalent to :

Kernel.Array(something)

this doesn't make sense to me. Since in the first case, the object main is of class Object, which got Kernel module mixed in, thus have the Array method. But in the second case, we are calling the Array method on the Kernel module object itself, rather than main object, didn't they are NOT the same thing?

sorry for my bad english.

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

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

发布评论

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

评论(4

浮光之海 2024-08-20 20:32:06

Kernel.Array 就是所谓的模块函数。模块函数的其他示例包括 Math.sin 和 Math.hypot 等。

模块函数是一种方法,它既是模块上的类方法,也是私有实例方法。当您在顶层调用 Array() 时,您将其作为主对象的私有实例方法进行调用。当您通过 Kernel.Array() 调用它时,您将其作为 Kernel 上的类方法进行调用。它们是相同的方法。

要了解更多信息,请阅读 ruby​​docs 中的 module_function 方法: http:// /www.ruby-doc.org/core/classes/Module.html#M001642

Kernel.Array is what is known as a module function. Other examples of module functions include Math.sin, and Math.hypot and so on.

A module function is a method that is both a class method on the module and also a private instance method. When you invoke Array() at the top-level you are invoking it as a private instance method of the main object. When you invoke it through Kernel.Array() you are invoking it as a class method on Kernel. They are the same method.

To learn more, read up on the module_function method in rubydocs: http://www.ruby-doc.org/core/classes/Module.html#M001642

岁吢 2024-08-20 20:32:06

class Object 混入模块 Kernel,但 Kernel 是 Object 的实例。所以内核“模块”方法 - 是它的实例方法。

class Object mixed-in module Kernel, but Kernel is an instance of Object. So Kernel "module" methods - is it's instance methods.

找回味觉 2024-08-20 20:32:06

让您感到困惑的是类方法和实例方法之间的区别。

类方法没有显式接收者,因此没有 self 来访问其他字段。他们只是……是。

通常,实例方法用于查询或操作给定对象的属性,而类方法是“帮助程序”或“工厂”方法,它们提供与某种类相关或特别有用的一些功能,但不依赖于实际的类。该类的实时实例(对象)。

不确定 Ruby,但 Java 有(例如)一个完整的类 Math,它只包含实例方法,例如 sin()max()、exp() 等等:不存在“数学”对象,这些只是体现数学算法的方法。这不是最好的例子,因为在 Ruby 中,这些方法可能作为实例方法直接嵌入到数字类中。

您提到的情况有点令人困惑,因为 Array() 方法和 KernelArray()< /code> 方法实际上是做类似事情的不同方法。两者都是类方法。

Array() 接受参数列表并创建并返回包含它们的数组。

Kernel.Array() 采用“可数组”类型的单个参数(例如序列),并采用该参数返回的值并从中构建一个数组。


更新

否决票也许是合理的;我很抱歉接受了我专业领域之外的主题。我想我很快就会删除这个答案。

@ Chuck: 我真诚地希望语言/库的官方文档能够提供一些关于它如何工作的有意义的线索。这是我在回答这个问题时咨询的。

Kernel.Array() 的 rdoc:

以数组形式返回 arg。首先尝试调用 arg.to_ary,然后调用 arg.to_a。如果两者都失败,则创建一个包含 arg 的单元素数组(除非 arg 为零)。

对于Array.()

返回一个用给定对象填充的新数组。

我不了解你的情况,但我认为如果文档差异很大,那么要么他们正在谈论单独的方法,要么文档就是一团糟。

@自由骑士:

但是 ruby​​ 中的所有内容都是某种类型的对象,甚至是类和模块。而 Kernel.Array 实际上是对特定对象(Kernel 对象)的方法调用。

是的,在幕后,Java 中也类似。但是 Array() 方法没有对 Kernel 执行任何操作,就像 Array() 没有对 Array 类对象执行任何操作一样,所以这实际上只是一个语义上的争论。它是一个实例方法,因为如果您足够疯狂,您可以将其挂在类 IPSocket 上,并且它仍然会以相同的方式工作。

What's confusing you is the difference between class and instance methods.

Class methods don't have an explicit receiver, and thus no self to access other fields with. They just... are.

Generally instance methods are used to query or manipulate the attributes of a given object, whereas the class methods are "helper" or "factory" methods that provide some functionality associated with or especially useful for a certain kind of class, but not dependent on actual live instances (objects) of that class.

Not sure about Ruby, but Java has (for example) a whole class, Math that contains nothing but instance methods like sin(), max(), exp() and so forth: There is no "Math" object, these are just methods that embody mathematical algorithms. Not the best example, because in Ruby those methods are probably embedded right into the numeric classes as instance methods.

The case you mention is a bit confusing because Array's () method and Kernel's Array() method are in fact different methods that do similar things. Both are class methods.

Array() takes a list of arguments and makes and returns an array containing them.

Kernel.Array() takes a single argument of an "array-able" type, such as a sequence, and takes the values returned by this argument and builds an array from those.


UPDATE

The downvote was perhaps justified; I apologize for taking on a subject outside my area of expertise. I think I'll be deleting this answer soon.

@ Chuck: I would sincerely hope that a language/library's official documentation would offer some meaningful clues as to how it works. This is what I consulted in answering this question.

The rdoc for Kernel.Array():

Returns arg as an Array. First tries to call arg.to_ary, then arg.to_a. If both fail, creates a single element array containing arg (unless arg is nil).

for Array.():

Returns a new array populated with the given objects.

I don't know about you, but I think if the docs vary that much then either they're talking about separate methods or the documentation is a train wreck.

@ freeknight:

But everything in ruby is an object of some kind, even classes and modules. And Kernel.Array is actually a method call on an specific object - the Kernel object.

Yeah, under the covers it's similar in Java too. But the Array() method isn't doing anything with Kernel, any more than Array() is doing anything with the Array class object, so this is really only a semantic quibble. It's an instance method because you could hang it off class IPSocket if you were crazy enough, and it would still work the same way.

风筝在阴天搁浅。 2024-08-20 20:32:06

它们是同一件事:

<块引用>

a = Kernel.Array('aa')
=> [“aa”]
a.类
=>数组
a = 数组('aaa')
=> [“aaa”]
a.类
=>数组

也许有一个别名?

They are the same thing:

a = Kernel.Array('aa')
=> ["aa"]
a.class
=> Array
a = Array('aaa')
=> ["aaa"]
a.class
=> Array

Maybe there is an alias?

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