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
4
votes
2 answers

ruby when to eigenclasses come into existence

Do Eigenclasses exist prior to a singleton method being defined on a Object or Class. ie Do they always exist or come in existence when a singleton method or class method in defined?
Godfather
  • 179
  • 2
  • 11
4
votes
1 answer

ActiveSupport::Callbacks on class methods

Is it possible to use ActiveSupport::Callbacks on class methods? More specifically, in the module below, where would you put include ActiveSupport::Callbacks to make define_callbacks and run_callbacks share the same state? module Handler extend…
Sim
  • 13,147
  • 9
  • 66
  • 95
4
votes
1 answer

What's the difference between a class and the singleton of that class in Ruby?

Okay, so I'm trying to do some metaprogramming in Ruby and I'm getting a bit confused. According to several articles I've read (like this one), in order to dynamically add class methods to Ruby classes, you have to use the class's singleton…
Ajedi32
  • 45,670
  • 22
  • 127
  • 172
3
votes
4 answers

Anonymous classes in Ruby

I have two questions: Does method f_1 belong to the metaclass anonymous class? Does method f_2 belong to the anonymous class? related to the following code: car = "car" class << car def self.f_1 puts "f_1" end def f_2 puts "f_2" …
user413881
  • 225
  • 1
  • 3
  • 10
3
votes
3 answers

What is a singleton class in Ruby?

I m having trouble understanding the concept of eigenclass or singleton class in ruby. I read a lot that the eigenclass is a class's class. That doesn't make sense to me as for me a class's class is actually Class as all classes are actually…
David Geismar
  • 3,152
  • 6
  • 41
  • 80
3
votes
0 answers

State-dependent behaviour of Python objects using Ruby-like eigenclasses with mixins

I've been looking for a natural way to implement state-dependent behaviour (state machines) in Python objects. The goal was for objects to have a small number of states, or "mutually orthogonal" aspects of state, which would determine their…
Alexey
  • 3,843
  • 6
  • 30
  • 44
3
votes
2 answers

Add methods from modules to specific instances of a class dynamically

Here's the deal: I need to extend specifica instances of the class Box with some methods. The methods i need to include live inside modules and i want the Box instance to be able to include the modules dynamically. Now i am using a hook with an…
Lucas d. Prim
  • 787
  • 1
  • 5
  • 17
3
votes
2 answers

Can a Ruby object have multiple eigenclasses?

Eigenclasses are added into the inheritance hierarchy. If multiple singleton methods are added, are these added to the same eigenclass, or different eigenclasses which are both injected into the inheritance hierarchy of that object? For example def…
Kevin Van Ryckegem
  • 1,915
  • 3
  • 28
  • 55
3
votes
1 answer

Can eigenclasses be enumerated?

Calling ObjectSpace.each_object(Class) does not seem to return any eigenclasses. For example, when investigating Ruby metaclasses: why three when defined singleton methods?, I found that while ObjectSpace.count_objects[:T_CLASS] was getting…
Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
3
votes
1 answer

Add class methods through include

I have Ruby class into which I want to include both class and instance methods. Following the pattern described here, I'm currently using the following: class SomeObject include SomeObject::Ability def self.some_builder_method(params) #…
user2398029
  • 6,699
  • 8
  • 48
  • 80
3
votes
4 answers

How to obtain the object corresponding to a given singleton (or eigen) class in Ruby?

Suppose the following code: class A end a = A.new As = class << a self end # or: # As = a.singleton_class Is there some way to get a from As?
MMSequeira
  • 61
  • 6
3
votes
3 answers

Knowing if a class is an eigenclass

What is the best way to tell if a class some_class is an eigenclass of some object?
sawa
  • 165,429
  • 45
  • 277
  • 381
2
votes
1 answer

Ruby eigenclass pattern - Asking for clarification

Which information sources describe best Ruby's eigenclasses? I have read the following: (see an extra page) Still, I was NOT able to deduce the following behaviour: class Object def sc(n = 1) # nth superclass if n == 0 then return self…
paon
  • 21
  • 1
2
votes
1 answer

Why fixnum doesn't have eigenclass?

This method is to return eigenclass of any object: class Object def eigenclass class << self; self; end end end Example for String: "abc".eigenclass # => #> Array: [1, 2].eigenclass # =>…
eugene
  • 550
  • 4
  • 15
2
votes
2 answers

Ruby - Share local variable with the obj's eigenclass

I am trying to dynamically define methods on an initialized Ruby object MyObject based on a hash my_hash I pass to its initialize method. In the body of the initialize method, I have the following: my_hash.each do |key| class << self …
n_x_l
  • 1,552
  • 3
  • 17
  • 34