Wintus
11/26/2016 - 5:31 AM

eigenclass.rb

module Extended
  refine Object do
    alias_method :eigenclass, :singleton_class

    # http://melborne.github.io/2012/07/12/object-repeat-take-care-of-sequence/
    def repeat(init = true) # assume &block is given
      x = self
      Enumerator.new do |y|
        y << x if init
        loop { y << (x = yield x) }
      end
    end
  end

  refine Class do
    alias_method :metaclass, :singleton_class
  end
end

using Extended

require 'pp'

# pp Class.singleton_class
pp Class.metaclass

puts

# pp Class.repeat(&:singleton_class).take(7)
pp *Class.repeat { |klass| klass.metaclass }.take(7)

puts

pp Class.repeat(&:itself).take(3)