Created
May 6, 2012 23:59
-
-
Save jfarmer/2625060 to your computer and use it in GitHub Desktop.
Eigenclasses in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In Ruby everything is an object, including classes. | |
# That means class methods are instance methods of some object. | |
# But what object? | |
class Object | |
def eigenclass | |
class << self | |
self | |
end | |
end | |
end | |
class Foo | |
def self.zzzz | |
"Sleepy time time, all the time. - Cream, 1966" | |
end | |
end | |
Foo.instance_methods.include?(:zzzz) # => false | |
Foo.methods.include?(:zzzz) # => true | |
Foo.eigenclass.instance_methods.include?(:zzzz) # => true | |
# And now for something completely different | |
Foo.class # => Class | |
Foo.superclass # => Object | |
Foo.eigenclass.class # => Class | |
Foo.eigenclass.superclass # => #<Class:Object> |
havenwood
commented
Oct 25, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment