Questions tagged [eigenclass]

In Ruby, an Eigenclass, also called a Singleton Class, is where methods defined on a specific object are actually stored. In the inheritance chain, it sits between an object and its class.

An Eigenclass, also known as a "Singleton Class," is an invisible class created by the Ruby interpreter when you define methods on a specific object. For example:

class American
  def greet
    puts "Nice to meet you!"
  end
end

lucy = American.new

# This method actually gets defined on 
# lucy's eigenclass
def lucy.greet       
  puts "Hi, I'm Lucy."
  # "super" tries to call a method by
  # the same name as this one, further
  # up the inheritance chain. In this
  # case, it will find American#greet
  super
end

lucy.greet 
# "Hi, I'm Lucy."
# "Nice to meet you!"

Further reading:

53 questions
1002
votes
6 answers

class << self idiom in Ruby

What does class << self do in Ruby?
randombits
  • 47,058
  • 76
  • 251
  • 433
86
votes
3 answers

Why isn't the eigenclass equivalent to self.class, when it looks so similar?

I've missed the memo somewhere, and I hope you'll explain this to me. Why is the eigenclass of an object different from self.class? class Foo def initialize(symbol) eigenclass = class << self self end eigenclass.class_eval do …
Robert K
  • 30,064
  • 12
  • 61
  • 79
14
votes
4 answers

Ruby Class Methods vs. Methods in Eigenclasses

Are class methods and methods in the eigenclass (or metaclass) of that class just two ways to define one thing? Otherwise, what are the differences? class X # class method def self.a "a" end # eigenclass method class << self def…
crispy
  • 5,737
  • 4
  • 33
  • 45
13
votes
3 answers

Is it possible to get all the eigenclasses in Ruby?

Getting a list of all modules is easy in Ruby: ObjectSpace.each_object(Module).to_a However, is it possible to get a list of all eigenclasses (also known as singleton classes or metaclasses)? Or are eigenclasses invisible? I tried str =…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
10
votes
1 answer

How to find private singleton methods

I have defined a module Vehicle like such module Vehicle class <
Mike Bradford
  • 240
  • 2
  • 10
9
votes
3 answers

Determine Class from Eigenclass

In Ruby, getting the eigenclass of a class Foo is a simple as eigenclass = class << Foo; self; end #=> # eigenclass = Foo.singleton_class #2.1.0 #=> # I'm interested in the inverse operation: getting the owner of the…
Chris Keele
  • 3,364
  • 3
  • 30
  • 52
9
votes
2 answers

Why a module's singleton method is not visible in downstream eigenclasses where it gets mixed?

I understand the regular method lookup path i.e. class, superclass/module, all the way up to BasicObject. I thought it was true for singleton version of the chain also but doesn't seem the case when you mixin a module in the meta-chain. I'd…
saihgala
  • 5,724
  • 3
  • 34
  • 31
8
votes
2 answers

Why is it important to learn about metaprogramming and eigenclasses in Ruby?

I am currently experimenting with Ruby and Rails, and I've hit a few sections in tutorials and books about metaprogramming. Many mention that it is an essential component of Ruby but they don't really go into detail. It's as if metaprogramming is…
dnatoli
  • 6,972
  • 9
  • 57
  • 96
7
votes
3 answers

Why am I able to use Kernel singleton methods like `puts`?

In Ruby, the method puts is a singleton method of the Kernel module. Normally, when a module is included or extended by another module, the module (but not its singleton class) is added to the inheritance tree. That effectively makes the instance…
rintaun
  • 697
  • 8
  • 14
6
votes
4 answers

Difference between 'self.method_name' and 'class << self' in Ruby

I was trying to limit the instantiation of a class to just a single one(without using singleton) but i couldn't. I tried with class variables (@@) but without luck. I googled it and came across this: class A @count = 0 class << self …
fk2blow
  • 155
  • 1
  • 10
5
votes
1 answer

Singleton class of singleton class of BasicObject in Ruby

This is mostly an “academic” one but here it goes: According to this Ruby eigenclass diagram (slightly edited): BasicObject.singleton_class.singleton_class.superclass is Class. However, running this on a Ruby interpreter (Ruby v2.5.1), it turns…
stratis
  • 7,750
  • 13
  • 53
  • 94
5
votes
1 answer

How to make a module constant also visible within an eigenclass?

I created a module that contains a constant NAME and a method hello. If a class includes the module, both definitions should be visible in different scope. module A NAME = 'Otto' def self.included(base) base.extend(ClassMethods) end def…
sschmeck
  • 7,233
  • 4
  • 40
  • 67
5
votes
3 answers

Class, Module, their eigenclasses, and method lookup

Let's open class Module and 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 class of Class is Class, whose superclass is Module. so it…
orange
  • 149
  • 7
4
votes
4 answers

Ruby class question

Possible Duplicate: class << self idiom in Ruby I have a quick Ruby question. I come from a Java/c background, so I understand in Ruby "self" when referenced inside a instance method acts like "this". And "self." prefix for method defines it as a…
kapso
  • 11,703
  • 16
  • 58
  • 76
4
votes
3 answers

Ruby eigenclass unexpected behaviour

First, let's add a method to retrieve eigenclass "copied from this blog post" class Object def eigenclass class << self self end end end Then create a simple class class A end puts A.new.eigenclass.superclass # => A puts…
bazaretas
  • 343
  • 8
  • 14
1
2 3 4