Class, module, their own points and method search

Open the class Moduleand add a method to it:

class Module  
  def foo  
    puts "phew"  
  end  
end

I can call this method by doing this,

Class.foo

which is understandable, because the class Classthere Class, which is a superclass Module. therefore, it can call instance methods defined in Module.

relation between classes and modules

Now the method barbelow is defined on Moduleeigenclass:

class Module  
   def self.bar  
     puts "bar"  
   end  
end

but now

Class.bar 

also works.

Can someone explain to me how Classcan access methods in Moduleeigenclass?


, . , . Class.foo, Class eigenclass, , Module eigenclass BasicObject eigenclass, (, ) Class ( Class BasicObject eigenclass), Module, .

, Class.bar, Class eigenclass, Module eigenclass, .

class Class   
  def check  
    puts "class instance method"  
  end
end   

class Module   
  def self.check    
    puts "modules eigenclass method"     
  end    
  def check    
    puts "module instance method"   
  end     
end

, wot - , :

Class.check 

: current understanding

+5
3

Ruby 2.0.

: , Ruby, singleton_class, eigenclass.

0

Ruby, Eigenclasses . , "Eigenclasses and Class Inheritance" , .

+1

I recently wrote a tutorial about my own eigenclassin Ruby:

enter link description here

0
source

All Articles