Module: Kernel

Defined in:
lib/tcravit_ruby_lib/on_execute.rb

Instance Method Summary collapse

Instance Method Details

#on_executeObject

A bit of ruby hackery to enable the script to be run directly from the command line, as well as included into other code. (Courtesy of elliottcable).

Example:

class MyApp
  # ...
  def start()
    #...
  end
end

on_execute do
  MyApp.new().start()
end


42
43
44
45
46
47
# File 'lib/tcravit_ruby_lib/on_execute.rb', line 42

def on_execute
  calling_file = caller.first.split(':').first
  if File.expand_path(calling_file) == File.expand_path($0)
    yield
  end
end