Output lines to stdout for debug

Discussion in 'Ruby' started by Andrew Walrond, Sep 3, 2003.

  1. 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
     
    Andrew Walrond, Sep 3, 2003
    #1
    1. Advertising

  2. Andrew Walrond

    mark Guest

    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
     
    mark, Sep 3, 2003
    #2
    1. Advertising

  3. Andrew Walrond

    Guest

    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
     
    , Sep 3, 2003
    #3
  4. 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
    >
     
    Andrew Walrond, Sep 13, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    614
    John Bentley
    Sep 10, 2003
  2. Elad
    Replies:
    0
    Views:
    437
  3. Andreas S
    Replies:
    3
    Views:
    287
    Eric Hodel
    Dec 9, 2006
  4. Marc Weber
    Replies:
    0
    Views:
    133
    Marc Weber
    Feb 17, 2011
  5. Replies:
    2
    Views:
    383
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page