Ruby Class Methods vs. Methods in Eigenclasses Ruby Class Methods vs. Methods in Eigenclasses ruby ruby

Ruby Class Methods vs. Methods in Eigenclasses


The two methods are equivalent. The 'eigenclass' version is helpful for using the attr_* methods, for example:

class Foo  @instances = []  class << self;    attr_reader :instances  end  def initialize    self.class.instances << self  endend2.times{ Foo.new }p Foo.instances#=> [#<Foo:0x2a3f020>, #<Foo:0x2a1a5c0>]

You can also use define_singleton_method to create methods on the class:

Foo.define_singleton_method :bim do "bam!" end


In Ruby there really are no such things as class methods. Since everything is an object in Ruby (including classes), when you say def self.class_method, you are just really defining a singleton method on the instance of the class Class. So to answer your question, saying

class X  def self.a    puts "Hi"  end  class << self    def b      puts "there"    end  endendX.a # => HiX.b # => there

is two ways of saying the same thing. Both these methods are just singeton (eigen, meta, ghost, or whatever you want to call them) methods defined in the instance of your Class object, which in your example was X. This topic is part of metaprogramming, which is a fun topic, that if you have been using Ruby for a while, you should check out.The Pragmatic Programmers have a great book on metaprogramming that you should definitely take a look at if you interested in the topic.


Yet another necromancer here to unearth this old question... One thing you might not be aware of is that marking a class method as private (using the private keyword instead of :private_class_method) is different than marking an eigenclass method as such. :

class Foo  class << self    def baz      puts "Eigenclass public method."    end    private    def qux      puts "Private method on eigenclass."    end  end  private  def self.bar    puts "Private class method."  endendFoo.bar#=> Private class method.Foo.baz#=> Eigenclass public method.Foo.qux#=> NoMethodError: private method `qux' called for Foo:Class#   from (irb)

The following example will work how the previous one intends:

class Foo  class << self    def baz      puts "Eigen class public method."    end    private    def qux      puts "Private method on eigenclass."    end  end  def bar    puts "Private class method."  end  private_class_method :barendFoo.bar#=> NoMethodError: private method `bar' called for Foo:Class#     from (irb)Foo.baz#=> Eigen class public method.Foo.qux#=> NoMethodError: private method `qux' called for Foo:Class#     from (irb)