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

Builder's XmlMarkup object loses constants?

I try to upgrade a legacy application from Ruby 1.8.7 to 2.2.3. Afterwards the rendering of builder templates raises errors about unknown classes. uninitialized constant Builder::XmlMarkup::BigDecimal (NameError) It seem that within the…
sschmeck
  • 7,233
  • 4
  • 40
  • 67
0
votes
0 answers

Are these just different ways of defining a static method in Ruby?

Based on what I know, yes they are, but I'm noob at ruby and want to be sure. Are there differences in what they do? Is there a reason to use one syntax over the other? I do know that class << self and def self.method behave differently if used…
Kashyap
  • 15,354
  • 13
  • 64
  • 103
0
votes
2 answers

RSpec unit testing an eigenclass: undefined method

I'm trying to test the method cumulative_cost in my Product model. #app/models/product.rb class Product < ActiveRecord::Base class << self def cumulative_cost self.sum(:cost) end end end So I'll run something like…
thedanotto
  • 6,895
  • 5
  • 45
  • 43
0
votes
3 answers

define_method and the object Class: strange behavior

EDIT: Let me expand a bit on what my intentions were: Given that a ruby object gets its methods from its class' instance methods. What I was trying to "prove" was that by adding instance methods to that object's class, they would then become method…
FullOfCaffeine
  • 539
  • 1
  • 6
  • 22
0
votes
2 answers

How does Inheritance work in Ruby?

According to Dave Thomas in his talk about the Ruby Object Model, there are no "class methods" in Ruby. There is only difference between whether the receiver of the method is a "class object" or an "instance object". class Dave def InstaceMethod …
zeronone
  • 2,912
  • 25
  • 28
0
votes
1 answer

Why defining a method in main defines it both in Object and in main.singleton_class?

I was checking to see where methods are defined in Ruby when the definition is performed at the top level and the result is surprising: def foo; end singleton_class != Object # => true self.class == Object # => true m1 =…
MMSequeira
  • 61
  • 6
0
votes
2 answers

Calling class method of eigenclass

I need to override a class method for some instances of the class class Foo def eigen_class class << self self end end def self.foo "original foo" end def override_foo class << self def self.foo …
synapse
  • 5,588
  • 6
  • 35
  • 65
0
votes
2 answers

Implicit context and eigenclasses

I recently found an interesting article on implicit contexts in Ruby, and I found the experience quite mind opening. I understood that Ruby holds a reference not only to self (the default method receiver), but also to the current class (also known…
Alberto Moriconi
  • 1,645
  • 10
  • 17
1 2 3
4