From: Andrew Walrond Date: 2003-09-13T23:14:25+09:00 Subject: Re: Output lines to stdout for debug Thanks - perfect! ----- Original Message ----- From: "mark" To: "ruby-talk ML" Sent: Wednesday, September 03, 2003 2:11 PM Subject: Re: Output lines to stdout for debug > On Wednesday 03 Sep 2003 11:06 am, Andrew Walrond wrote: > > Is there an easy way of getting each line of a ruby script to be written to > > stdout before being executed? > > I currently use a wrapper function which takes the command as a string and > > puts()'s then eval()'s it, but thats very ugly. > > > > Any suggestions? > > > > The following function will do the trick > > def trace_program(file) > lines = File.readlines(file) > set_trace_func proc do |event, file, line, *extra| > puts lines[line - 1] if event == "line" > end > end > > This reads the file into an array then sets up a system hook to get the line > number of each line before it's executed, and prints the line from the array. > > Then you do something like > > trace_program(__FILE__) > > puts "x" > puts "y" > puts "z" > > which outputs > puts "x" > x > puts "y" > y > puts "z" > z > > Though there is probably a more Rubyist way to do this. > > > Andrew Walrond > > Best Regards > > Mark Sparshatt >