Skip to content

Instantly share code, notes, and snippets.

@jfarmer
Created May 6, 2012 23:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfarmer/2625060 to your computer and use it in GitHub Desktop.
Save jfarmer/2625060 to your computer and use it in GitHub Desktop.
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
Copy link

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