I have been very tempted recently to give up JRuby in favour of Python.
Part of the reason is that I have been dabbling with Arduinos and the
Arduino community is more likely to be familiar with Python. And the
other part is the JRuby startup delay which is painfully obvious after
using Python for an hour.
Then it occurred to me that the startup delay could be avoided by
preventing JRuby from exiting when a program finished.
This short code is a first crude attempt to achieve that and it seems to
work. Indeed it runs the short test program so fast that I lose track of
whether I have pressed return. And it seems to catch load errors, syntax
errors, runtime errors and java errors (in a much larger program that I
tried).
I have little doubt that a more experienced JRuby programmer will see
scope for many improvements.
It would be nice if every possible exception could be trapped with a
single RESCUE clause - but I don’t know how to do that.
==========
ScriptRunner.rb
def runScript
begin
# load is used as require would only load the script once
load $scriptName
# assumes script contains a method called scriptStart
scriptStart
rescue ScriptError # catches load and syntax errors
puts "SCRIPT ERROR A"
puts $!
rescue NativeException # catches Java errors
puts "SCRIPT ERROR B"
puts $!
rescue # catches runtime errors
puts "SCRIPT ERROR C"
puts $!
end
end
puts
puts "Starting ScriptRunner"
$scriptName = "./test.rb"
while true
puts
puts "Press Return"
xxx = gets
runScript
puts "Done"
end
=============
test.rb
def scriptStart
puts "Hey - here is testing"
xxx = 8 / 1
puts xxx
end