On 2 ον, 16:30, Ilias Lazaridis <il... / lazaridis.com>rote: > The construct to detect execution of the file (in order to launch main > code) looks like this: > > if __FILE__ == $0 > main > end > > I would like a more elegant code. > > I try to implement a method in Kernel > > #myib.rb > module Kernel > def executed? > # detect in a clean way if the caller calls from an executed file > # return true if so, else return false > end > end > > #maintest.rb > require 'mylib' > if executed? > print "works nice" > else > print "seems to fail > end [...] All 3 suggestion (Zotov, Galn, Higgins) failed on my site for different reasons (e.g regex with ":" hits on P:/ on windows). Other problems where with inclusions. Using several constructs I saw, I came up with this one: #mylib.rb module Kernel def executed? re = /<(.*?)>/ # ? non-greedy, stops on first match caller.to_s.match re # matches first text within <> in call-stack #print caller.to_s return $1 == "main" # first match was "main" means "executed" end end if executed? print "EXEC mylib\n" end #mytest.rb require_relative 'mylib' if executed? print "EXEC mytest\n" end #maintest.rb require_relative 'mylib' require_relative 'mytest' if executed? print "EXEC maintest\n" end - ruby mylib.rb => EXEC mylib ruby mylib.rb => EXEC mytest ruby maintest.rb => EXEC maintest - I still dislike this solution, as it's not clean. Can it be expected to work stable (MRI 1.9.x is my main target)? . -- http://lazaridis.com