Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

Ruby Error - but where? - aargh!!

Post any examples or modules that you want to share here

Ruby Error - but where? - aargh!!

Postby trogluddite » Sat Mar 16, 2013 2:03 am

Yes, I'm sure any of us that have played with Ruby have been there - got that new Ruby command sussed out, type in the code, and then in the little box at the bottom of the editor...

"ErrorMessage: Your code is rubbish, you can't expect me to do that!"

Dammit - but where the hell is that bug? There are dozens of lines of code to check, and my 'event' method is calling my 'doCoolStuff' method, which does a redraw of the 'draw' method - how the hell am I supposed to find it ??!?!?!
Often the message will tell you a method name where the error happened - but it doesn't often help because it is just the place where you called something - many times it's the code you're calling at the other end that's the problem. And there's not even a line number so you know where to start looking!

Well, here's a sneaky trick that you can use to get Ruby to be a little more helpful - just three extra lines of code to pop onto the end of a "def....end" block here and there...
Code: Select all
def myMethod

  # Extra Cool funky code
  # goes here

rescue => error
  watch "Error help",error.backtrace
  raise error
end


Note the three lines of code just before 'end' (they have to go right there at the end).
Now, if there is an error in the method, you'll get the usual cryptic error message - but also a handy little printout of the backtrace.
"Backtrace" - what's that?
The backtrace is a list of all the method calls that Ruby made up to the point of there being an error, and looks something like this...

["(eval):3:in myMethod", "(eval):13:in event"]

So what does that mean?
Each little string in the list is one of the steps that Ruby took - beginning with the one closest to where the error happened. And each has the same format...
(eval) - ignore this bit - it just means that Ruby evaluated some new code.
Number - a line number in the code - actually it's one line after the one you're looking for.
in <name> - the name of the method that Ruby was running.

By following the chain of line numbers, you can follow the path taken by whatever value it was that caused the problem, checking each line number in turn for mistakes, or to find out which variable it was that had the wrong value.
Because of the way that Ruby handles errors, you don't even need this code in every single method. In most cases, our variable values start their life either from the 'event' method reading inputs, reDraws firing a 'draw' method, or mouse moves - so you only really need the extra code at the end of those methods. However, you can't get a backtrace if your Ruby is just "bare code", only if you are using method definitions ("def...end").

Here's an example showing what I mean - try typing zero into the integer input box...
Error tracing.fsm
(355 Bytes) Downloaded 347 times

You will see from the 'backtrace' just how Ruby keeps a record of each step - where the divide caused an error, the line where the 'divide' method was called, and where the 'test' method was called by 'event' - each with a line number (+1) of where each call was made. Following that chain backwards, we end up at the "test(@in)" statement, and can reason that @in must be the variable with the rogue zero.

Juts be careful to type that code accurately - a mistake in those three lines could make the error reporting even more confusing, or even stop you from getting error messages at all!!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1206
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Ruby Error - but where? - aargh!!

Postby Nubeat7 » Sun Mar 17, 2013 11:00 am

thanks trog, thats a really helpful thing! often i wished that there is something like in the programming ide`s where you can see the linenumber you did a mistake or you can jump to there with a dobbleclick on the report.... this little piece of code brings us now closer to this.

btw is there a way to show the linenumbers in the code too? it would be really helpfull to not count the lines down to the error code, which could be really a pain in the ass when you have code above 50 lines..
User avatar
Nubeat7
 
Posts: 1388
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: Ruby Error - but where? - aargh!!

Postby nix » Sun Mar 17, 2013 11:08 am

You could build a parser(is that the right term),
with FS, using strings
User avatar
nix
 
Posts: 759
Joined: Tue Jul 13, 2010 10:51 am

Re: Ruby Error - but where? - aargh!!

Postby Nubeat7 » Sun Mar 17, 2013 11:25 am

oh you mean something which prints out the code with linenumbers, yes, also copy and paste the code into an editor should do this :idea:
http://www.flos-freeware.ch/notepad2.html shows the linenumbers!
User avatar
Nubeat7
 
Posts: 1388
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: Ruby Error - but where? - aargh!!

Postby trogluddite » Sun Mar 17, 2013 3:17 pm

Nubeat7 wrote:also copy and paste the code into an editor should do this

Yes, that's the way I do it too in Notepad++. Would be nice to have the numbering in the FS editor too though.
I'd recommend anyone who's into Ruby to download one of the free editors - if only because the curly brackets are so hard to tell apart with the FS editor font! Don't know about the others, but I really like the way Notepad++ lets you have a load of different views of the same code - saves loads of scrolling about when you can't remember what the hell you called your variables! And search/replace too - I find it hard to live without that.
If I get time, I'm going to see if I can do a custom Ruby syntax file for NP++ that highlights the FS keywords - I did one before for assembly, and it wasn't too difficult.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1206
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Ruby Error - but where? - aargh!!

Postby Walter Sommerfeld » Tue Aug 26, 2014 4:40 pm

Only wanna say thank u trog... ;-)

a little late - i know :lol:
In combination with MyCo's perfect RubyEdit Code Output i just found a bug in my little 150 ruby lines of code :mrgreen:

keep on doing!

Walter
User avatar
Walter Sommerfeld
 
Posts: 259
Joined: Wed Jul 14, 2010 6:00 pm
Location: HH - Made in Germany


Return to User Examples

Who is online

Users browsing this forum: No registered users and 9 guests