2007-09-03

Black magic

Today when I refactored some of my legacy code, I accidentally ran into the problem that I tried to let the outer class inherit from an inner class, like: class OuterClass < OuterClass::InnerClass. I mentioned in on #ruby-lang, mostly as an anecdotal remark, but LoganCapaldo actually came up with a solution for it:
parent = Class.new
Foo = Class.new(parent)
Foo.class_eval { Foo::Bar = parent }

class Foo < Foo::Bar
class Bar
def a
1
end
end
end

p Foo.new.a

Now I don't know if I should bow before such ingenuity or run away screaming from this darkest of the black coding magics :)

No comments: