7

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.

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for what looks to be a very helpful explanation. Can you please address/explain the difference in documentation mentioned in my answer?
@Carl, what documentation are you reading? but it's true that many ruby docs leave a lot to be desired
<ruby-doc.org/core/classes/Array.html> and <ruby-doc.org/core/classes/Kernel.html>. Horse's mouth, as it were. I think rdoc could use some mechanism for automating these intentional identical duplicates.
0

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

3 Comments

this cannot be right because the Array method is a private instance method and so would not be accessible directly on Kernel. Try it on any other object => Object.new.Array #=> private method error
yes, you are right - in Object its private instance method (but in Kernel its public). I don't see any variant except class << Kernel public :Array end
to your post "When you invoke it through Kernel.Array() you are invoking it as a class method on Kernel. They are the same method." In documentation method Array is a Public Instance method of Kernel, and problem is how it becomes Kernel public class method...
0

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.

6 Comments

The receiver of class methods is the Class singleton that represents the class.
In general, class method is indeed some kind of "factory" or "helper" and that's especially true in Java. 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.
In other words, there is a self within class methods. Also, I see no evidence that the two Array()s are different methods, and neither appears to be a class method.
Chuck, Array is a module_function (as stated in my answer) and so is a class method on Kernel as well as a private instance method on Kernel. (for proof check out the definition of rb_define_global_function in class.c)
@Martinho, that's incorrect. The receiver of a class method is the CLASS. However the class method must be defined on the Class's singleton.
|
-2

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.