Skip to content

Instantly share code, notes, and snippets.

@spastorino
Created October 19, 2010 22:31
Show Gist options
  • Save spastorino/635283 to your computer and use it in GitHub Desktop.
Save spastorino/635283 to your computer and use it in GitHub Desktop.
require 'fiber'
module Eigenclass
def eigenclass
class << self; self end
end
module_function :eigenclass
public :eigenclass
end
class Object
include Eigenclass
end
class EigenclassesGenerator
def initialize(obj)
@eigenclasses = [obj.eigenclass]
@fiber = Fiber.new do
loop do
Fiber.yield @eigenclasses.last
@eigenclasses[@eigenclasses.length] = @eigenclasses.last.eigenclass
end
end
end
def [](index)
(index - @eigenclasses.length + 2).times { @fiber.resume } if index >= @eigenclasses.length
@eigenclasses[index]
end
end
# examples
eg = EigenclassesGenerator.new(String)
p eg[3]
p eg[5]
p eg[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment