Use the caller method and parse it for the file name. inherited always called from the file defining the class.
Consider the following:
a.rb:
require 'pp' class Base def self.inherited(subclass) pp caller end end
b.rb:
require './a.rb' class Derived < Base end
Run this:
$ruby b.rb ["b.rb:3"]
source share