This is not possible. Consider this: when should a class definition be considered "done" in Ruby?
Instead of self.inherited
, I would personally make a class method called finalize!
and put your post-class-creation routine in there.
# A silly, contrived example
class A
def self.author(person)
@@authors ||= Array.new
@@authors << person
end
def self.finalize!
@@authors.map! { |a| Author[a] }
end
end
class B < A
author "Jason Harris"
author "George Duncan"
finalize!
end
You can probably get away with making a wrapper function instead:
class A
def self.define_authors(&blk)
yield
# (...finalize here)
end
...
end
class B < A
define_authors {
author "Jason Harris"
author "George Duncan"
}
end
...Or do consider that there may be ways where that finalizing step may not be needed.