Eigenclass in Ruby
References:
- http://www.integralist.co.uk/posts/eigenclass.html
- https://gist.github.com/Integralist/bb8760d11a03c88da151
When creating singleton methods (i.e. def self.foo) Ruby will create an anonymous class to hold these methods. The anonymous class then assumes the role of the object’s class and the original class is re-designated as the superclass of that anonymous class (Eigenclass).
privateis actually a methodprivatemethod only changes the visibility of instance methodsClass methods on the other hand are instance methods of the Eigenclass
1class Test
2
3 def self.bar
4 puts "bar"
5 end
6
7 def self.foo
8 puts "foo"
9 end
10
11 private_class_method :foo, :bar
12
13end
14
15Test.bar
16Test.foo