From: Ilias Lazaridis Date: 2011-06-04T00:35:36+09:00 Subject: Re: CORE - Replace "if __FILE__ == $0" with "executed?" On 3 Éïýí, 17:17, Chuck Remes wrote: > On Jun 3, 2011, at 9:05 AM, Ilias Lazaridis wrote: > > 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 correction: ruby mytest.rb => EXEC mytest > > ruby maintest.rb => EXEC maintest > > > - > > > I still dislike this solution, as it's not clean. > > What isn't "clean" about it? [...] I am criticising myself. My implementation is not "clean", as it depends on the call stack / regex. . -- http://lazaridis.com