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

What would be the code for executed?

I tried this way:
def executed?
    f = eval '__FILE__', TOPLEVEL_BINDING
    p = eval '$0', TOPLEVEL_BINDING
    return  f == p
end

but this failed somehow, and I dislike it.

.

--
http://lazaridis.com