Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Eigenclasses in Ruby
# 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

This comment has been minimized.

Copy link

commented Oct 25, 2016

anon = Class.new

anon #=> #<Class:0x...>
anon.singleton_class #=> #<Class:#<Class:0x...>>

Foo = anon

anon #=> Foo
anon.singleton_class #=> #<Class:Foo>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.