Output lines to stdout for debug

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?

Andrew Walrond

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

···

On Wednesday 03 Sep 2003 11:06 am, Andrew Walrond wrote:

Hi,

···

At Wed, 3 Sep 2003 19:06:05 +0900, 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.

tracer.rb


Nobu Nakada

Thanks - perfect!

···

----- Original Message -----
From: “mark” msparshatt@yahoo.co.uk
To: “ruby-talk ML” ruby-talk@ruby-lang.org
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