Learning to Backtrace

In an effort to improve my coding habits and knowledge of Ruby I am learning how to handle errors. While using backtrace in Aptana it shows the offending filename, line number and method. However in RPG maker with the same code the file name part shows what appears to be the script index instead of the name. The RPG maker help file says that backtrace should output like so:

"#{sourcefile}:#{sourceline}:in `#{method}'"I'll use the following script as an example:

Spoiler
class Test def self.run(arg1) num = 5 num /= arg1 raise ZeroDivisionError, "arg1 must not equal 0 or 0.0\n" if arg1 == 0.0 return num rescue ZeroDivisionError => e print e.message e.backtrace.join("\n") endendclass Test2 def self.run2 puts Test.run(0) endendTest2.run2
This in Aptana outputs:

arg1 must not equal 0 or 0.0C:/Users/Name/Documents/Aptana Studio 3 Workspace/testing/testfile.rb:19:in `run'C:/Users/Name/Documents/Aptana Studio 3 Workspace/testing/testfile.rb:29:in `run2'C:/Users/Name/Documents/Aptana Studio 3 Workspace/testing/testfile.rb:33:in `<main>'In RPG maker it outputs as:

arg1 must not equal 0 or 0.0{0128}:19:in 'run'{0128}:29:in 'run2'{0128}:33:in '<main>'So then how can I use backtrace to output something like:

arg1 must not equal 0 or 0.0Testfile:19:in 'run'Testfile:29:in 'run2'Testfile:33:in '<main>'Thanks for any help on this. 🙂
I'm on my phone, so I'm going to keep this short, but $RGSS_SCRIPTS[id][1] I believe. Just check out the $RGSS_SCRIPTS array.
Thanks 😀 !

By the way wondering if someone could explain (or recommend a good book) on how backtrace comes up with it's information.
I can't explain it myself, but I know that Tsukihime linked to an operating backtracer script on her blog, you might want to check that script how it works:


http://himeworks.com/2013/06/custom-main-full-error-backtrace/
Thanks Andar for linking that, explains how it works pretty well. 🙂

Edit: My method is strangely simpler though (as I couldn't figure out set_backtrace for line and method calls before you posted) so I went with this :

  rescue ZeroDivisionError => e    msgbox(e.message, e.backtrace[0..e.backtrace.size-2].join("\n").gsub(    /({\d*})/i) { |id| "From: #{$RGSS_SCRIPTS[id[1..4].to_i][1]}"})Since it was already doing the line number and method call thought well might just be easy to gsub the {0128} out replace with the script name and use that number to find which scripts were causing the problem.

Original thread: https://forums.rpgmakerweb.com/threads/learning-to-backtrace.45267/