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?
cla
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
end
end
2.times{ Foo.new }
p Foo.instances
#=> [#, #]
You can also use define_singleton_method
to create methods on the class:
Foo.define_singleton_method :bim do "bam!" end