2

I'm wondering about definition of method in top level.

puts RUBY_VERSION #=> 2.1.5
def greet
  "hello, world"
end
class Object
    greet #=> "hello, world"
end

Why does greet act like private class method?

puts RUBY_VERSION #=> 2.1.5
def greet
  "good morning"
end

# case 1
Object.private_methods(false).grep /greet/ #=> []
# case 2
Object.private_instance_methods(false).grep /greet/ #=> [:greet]
# case 3
Object.private_methods(true).grep /greet/ #=> [:greet]

In case 3, I found that greet is a private class method. But I'm wondering which class owns greet as a private class method. Object inherits itself?

Edit

I update my question.

Question #1

Does definition of method mean adding some methods in Object as private instance method.
Is this correct ?

Question #2

Object is an instance of Class. So, Object owns private class methods. These methods as private instance methods in Class.
Is this correct ?

Question #3

depends on question #1 and #2. Class inherits Object. So, Class owns greet as private class method and private instance method. Is this correct ?

tkobe
  • 21
  • 4

2 Answers2

2

Why does greet act like private class method?

It doesn't. It acts like a private instance method. (In fact, there are no class methods in Ruby, only instance methods. The question is not whether a method is an instance method or a class method, rather it's what class the instance method is defined in.)

Methods defined at the top-level become private instance methods of Object.

# case 1
Object.private_methods(false).grep /greet/ #=> []
# case 2
Object.private_instance_methods(false).grep /greet/ #=> [:greet]
# case 3
Object.private_methods(true).grep /greet/ #=> [:greet]

In case 3, I found that greet is a private class method.

Like I said above: there are no class methods in Ruby.

greet is a private instance method of Object. Object is an instance of Class, Class is a subclass of Module, Module is a subclass of Object, ergo Object is an instance of Object i.e. itself.

Put another way: methods defined in Object are available for all objects. Classes are objects, too, ergo methods defined in Object are available for all classes, including Object.

But I'm wondering which class owns greet as a private class method.

None. There are no class methods in Ruby. greet is a private instance method of Object.

Object inherits itself?

No. Object is an instance of itself.

Question #1

Does definition of method mean adding some methods in Object as private instance method.
Is this correct ?

Methods defined at the top-level become private instance methods of Object.

Question #2

Object is an instance of Class. So, Object owns private class methods. These methods as private instance methods in Class.
Is this correct ?

I cannot parse your question, sorry.

However, there are no private instance methods of Class in your examples. The only method in your example is greet, which is a private instance method of Object, not Class.

If you want to know who owns a method, you can just ask Ruby:

method(:greet).owner
# => Object

Question #3

depends on question #1 and #2. Class inherits Object. So, Class owns >greet as private class method and private instance method. Is this correct ?

No. There are no class methods in Ruby, only instance methods. And greet is not owned by Class, it is a private instance method of Object:

method(:greet).owner
# => Object
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
1

Using irb, defining greet at the top-level does NOT define greet as a private method, at least using ruby 2.x:

$ irb
> RUBY_VERSION
=> "2.3.0"
> def greet; "hi"; end
=> :greet

> Object.methods.grep /greet/
=> [:greet]

> Object.private_methods.grep /greet/
=> []
> Object.private_instance_methods.grep /greet/
=> []

In ruby, a "private method" is one that cannot be called with an explicit "receiver" in the usual way. (See e.g. this SO page.) Since self.greet and Object.greet both produce the same result as greet, it follows that greet (defined at the top-level in irb) cannot be a private method.

The situation is different when using the ruby compiler.

Community
  • 1
  • 1
peak
  • 105,803
  • 17
  • 152
  • 177
  • 1
    oops! I tried with ruby 2.1.5. But i tried again with ruby 2.4.0. I found same results – tkobe Mar 24 '16 at 06:36
  • 1
    That's a well-known, age-old bug in IRb. Do not rely on IRb for inferences about the behavior of Ruby. – Jörg W Mittag Mar 24 '16 at 15:13
  • The IRB binds methods in the top level scope to `main` as public methods for convenience (and that's weird). If you run your code above as a script with `ruby YOUR_FILE_NAME` then `greet` will be a `private` method – molexi Jan 10 '21 at 17:17