Here you go:
require "log"
abstract class Parent
Log = ::Log.for self
macro inherited
Log = ::Log.for self
def log
Log
end
end
def initialize
log.info { "hello" }
end
def foo
log.info { "foo" }
end
end
class Child < Parent
end
class Deep < Child
end
Child.new.foo
Deep.new.foo
Note that I needed to define a method log
inside each type. The reason is that if we just use the base methods referring to Log
, these are statically bound to the type where they are used (Parent).
That is, when you write SomeType
in a method, it will always point to the type resolved in that method, never in a child. (this is the same as in Ruby)