17

Is there a way to have a ruby script echo back (or log to file I can tail -f) every line executed, similar to bash -x or @echo on in DOS?

ruby -w doesn't do it--only increases verbosity of warnings etc.

Researched Unroller but it doesn't work, possibly too dated. Uncompilable dependencies.

I use irb a lot but in this case I need something non-interactive eg. to inspect post-mortem.

Marcos
  • 4,796
  • 5
  • 40
  • 64

2 Answers2

9

You can use

ruby -rtracer [your_script.rb]

There is also ruby-debug which can do

rdebug --trace [your_script.rb]
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
  • Tried -rtracer, nothing special prints. Apparently broken in Ruby 1.9.1 (can't yet upgrade due to fragile Ubuntu packages vs. Rubygem dependency fights, sadly) But thank you, would be better than irb printing every comment and non-exec line – Marcos Dec 19 '11 at 20:00
  • What a serious thorn trying to get rdebug/ruby-debug going on an Ubuntu server (10.10 upgraded from 10.04 from 9.*). No Apt package. {gem,gem1.9.1} install ruby-debug gives only incompatible version outside path: /var/lib/gems/1.8/bin/rdebug – Marcos Dec 19 '11 at 21:53
  • 1
    What about compiling Ruby 1.9.3 by yourself in /opt/ruby193 or something. It's not too hard and could be worth the effort. There's also [RVM](http://beginrescueend.com/) that helps managing multiple Ruby installations. – Niklas B. Dec 19 '11 at 22:07
  • In addition to outputting each statement that is executed, this flag will also output the line number and source file for each statement! It looks like `#0:square_proof.rb:4::-: num = 15 + (i * 100)` when I do it in Ruby 2.0.0. – Kevin Apr 26 '15 at 05:47
4

IRB does the trick:

irb script.rb
chrish.
  • 715
  • 5
  • 11
  • Thank you--I knew I was missing something ridiculously simple. – Marcos Dec 19 '11 at 19:26
  • It is NOT like `set -x`. I've stopped using irb dumps since it uselessly prints my entire classes and methods (1000's of lines of code) at the start just because they're defined up front, even comments and comment blocks, not (just) because lines are executing in real-time. – Marcos Jan 23 '12 at 20:36
  • I like this answer. It works well. The only downside is that it's IRB which is pretty lackluster in features. Is there a way to do it using Pry or an alternate debugger? – Nitrodist Aug 15 '19 at 16:58