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
2
votes
1 answer

Ruby looks for class variable in the Object instead of specific class

This part works: class Example1 @@var1= "var1 in the Example1" def get_var1 @@var1 end end example1 = Example1.new example1.get_var1 # => "var1 in the Example1" but if I try eigenclass: def example1.get_var1 …
Darek Nędza
  • 1,420
  • 1
  • 12
  • 19
2
votes
1 answer

Setting instance defaults in the eigenclass of ruby classes

This is one way I've managed to accomplish this. class Test class << self attr_accessor :stuff def thing msg @stuff ||= "" @stuff += msg end end def initialize @stuff = self.class.stuff puts @stuff end end #…
Senjai
  • 1,811
  • 3
  • 20
  • 40
2
votes
1 answer

Ruby class << Klass = Module::new

I have across the following syntax in a code snippet, and I'm not sure what it does. class << PushableModule = Module::new def new *args, &blk m = Module::new( *args, &blk ) m.extend Pushable m end end First off, the class or module…
Daniel Brady
  • 934
  • 3
  • 11
  • 27
2
votes
1 answer

Ruby 1.8 vs. 1.9: Determining whether a Ruby singleton class is a subclass of another class

As mentioned in an answer to a previous question, B < A is the easiest way to determine whether a class is a subclass of another. However, this appears to fail in Ruby 1.8 (also REE 1.8) when B is a singleton class (i.e., eigenclass). To…
Claw
  • 767
  • 8
  • 22
2
votes
1 answer

Ruby - Is there a way to get the instance of an eigenclass?

I am looking for a way to get the instance of an eigenclass, since each eigenclass has only one instance. I could look thru ObjectSpace be testing each eigenclass, but I guess it's expensive. Curiously, I must get the eigenclass of each object to…
Sony Santos
  • 5,435
  • 30
  • 41
1
vote
2 answers

Metaprogramming: Where does method dispatch look when we call an instance method that is defined inside a class method?

I'm reading Metaprogramming Ruby and just want to clear something up about the following paraphrased code: class MyClazz def self.my_class_method(name) define_method(name) { # do stuff } end my_class_method :foo …
stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
1
vote
1 answer

Initializing class instance variables of subclasses from the superclass

Given the superclass in the code below, I want all subclasses to have some instance variable. The code below does that, but fails to properly initialize that variable for all possible subclasses. I opened the eigenclass of my superclass. Here is the…
Logain
  • 4,259
  • 1
  • 23
  • 32
1
vote
1 answer

Having a module singleton extending a class

I have a singleton in my application that gets reused across applications. I want that singleton to get some default methods from my class, but also be able to customize the module/eigenclass. Most of all, I don't want to call up instance on every…
Jonathan Allard
  • 18,429
  • 11
  • 54
  • 75
1
vote
1 answer

Why is `class << self` more common than `class << Foo` for opening a class object's eigenclass?

Ruby programmers typically use class << self inside a class body to open up the class object's eigenclass, like so: class Foo class << self # ... end end However, I seldom see this equivalent form (assume that Foo is already defined to be a…
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
1
vote
1 answer

Ruby eigenclass (sigleton class) created? For which?

Got confused on Ruby meta-programming. So from the tutorial I learnt the following: cat = "kitty" cat.upcase # "KITTY" cat.poop # undefined 'poop' def cat.poop puts 'pooooooo...' end cat.upcase # "KITTY" cat.poop # "pooooooo..." So this…
Bruce
  • 1,608
  • 2
  • 17
  • 29
0
votes
1 answer

About class definition in Ruby

Recently, I was investigating into some details about classes in Ruby, and was confused by class definition. In Ruby, the class definition is as follows, class A def self.my_method end end and it's the same as class A class << self …
Davidsun
  • 721
  • 6
  • 13
0
votes
0 answers

How to extend Eigen's Tensor class?

I have created a function to display the shape of a Tensor on eigen. template void shape(const Eigen::Tensor& x) { cout << "( "; for (int i(0); i
0
votes
0 answers

How can you instantiate or clone/wrap a module in ruby?

I know by default Ruby modules are not instantiate. However I am using a gem with this singleton interface and I need two instances of it since there are two distinct configurations I need. Is there a way to deep clone or instantiate a second object…
Teddy
  • 55
  • 2
  • 9
0
votes
1 answer

How to create a method that executes a previously given block in Ruby?

I have a class that was built for subclassing. class A def initialize(name) end def some # to define in subclass end end # usage p A.new('foo').some #=> nil In my use case, I don't want to create a subclass since I need just one…
sschmeck
  • 7,233
  • 4
  • 40
  • 67
0
votes
1 answer

Ruby: provide real world examples when have you opened objects eigenclass and changed it

Coming from C# world I am used to thinking classes are immutable definitions of objects and that every object has fixed class. I am trying to open my mind to possibilities of using class << some_object def something_unique_to_this_object #…
Marko Avlijaš
  • 1,579
  • 13
  • 27