How do I debug Ruby scripts?

2019-01-01 06:37发布

I copied the following Ruby code from the Internet and made a few changes.

#insert code here

But it doesn't work!

Please help. What can I do to debug the program by myself?

17条回答
初与友歌
2楼-- · 2019-01-01 07:08

I strongly recommend this video, in order to pick the proper tool at the moment to debug our code.

https://www.youtube.com/watch?v=GwgF8GcynV0

Personally, I'd highlight two big topics in this video.

  • Pry is awesome for debug data, "pry is a data explorer" (sic)
  • Debugger seems to be better to debug step by step.

That's my two cents!

查看更多
只靠听说
3楼-- · 2019-01-01 07:08

I just discovered this gem ( turns Pry into a debugger for MRI Ruby 2.0+ )

https://github.com/deivid-rodriguez/pry-byebug

break SomeClass#run            # Break at the start of `SomeClass#run`.
break Foo#bar if baz?          # Break at `Foo#bar` only if `baz?`.
break app/models/user.rb:15    # Break at line 15 in user.rb.
break 14                       # Break at line 14 in the current file.
查看更多
流年柔荑漫光年
4楼-- · 2019-01-01 07:09

To easily debug Ruby shell script, just change its first line from:

#!/usr/bin/env ruby

to:

#!/usr/bin/env ruby -rdebug

Then every time when debugger console is shown, you can choose:

  • c for Continue (to the next Exception, breakpoint or line with: debugger),
  • n for Next line,
  • w/where to Display frame/call stack,
  • l to Show the current code,
  • cat to show catchpoints.
  • h for more Help.

See also: Debugging with ruby-debug, Key shortcuts for ruby-debug gem.


In case the script just hangs and you need a backtrace, try using lldb/gdb like:

echo 'call (void)rb_backtrace()' | lldb -p $(pgrep -nf ruby)

and then check your process foreground.

Replace lldb with gdb if works better. Prefix with sudo to debug non-owned process.

查看更多
一个人的天荒地老
5楼-- · 2019-01-01 07:12

As of Ruby 2.4.0, it is easier to start an IRB REPL session in the middle of any Ruby program. Put these lines at the point in the program that you want to debug:

require 'irb'
binding.irb

You can run Ruby code and print out local variables. Type Ctrl+D or quit to end the REPL and let Ruby program keep running.

You can also use puts and p to print out values from your program as it is running.

查看更多
琉璃瓶的回忆
6楼-- · 2019-01-01 07:12

deletes all the things

Welcome to 2017 ^_^

Okay, so if you're not opposed to trying out a new IDE you can do the following for free.

Quick Instructions

  1. Install vscode
  2. Install Ruby Dev Kit if you haven't already
  3. Install Ruby, ruby-linter, and ruby-rubocop extensions for vscode
  4. Manually install whatever gems rubyide/vscode-ruby specifies, if needed
  5. Configure your launch.json to use "cwd" and and "program" fields using the {workspaceRoot} macro
  6. Add a field called "showDebuggerOutput" and set it to true
  7. Enable breakpoints everywhere in your Debug preferences as "debug.allowBreakpointsEverywhere": true

Detailed Instructions

  1. Download Visual Studio Code aka vscode; this is not the same as Visual Studio. It's free, light-weight, and generally positively regarded.
  2. Install the Ruby Dev Kit; you should follow the instructions at their repo here: https://github.com/oneclick/rubyinstaller/wiki/Development-Kit
  3. Next you can either install extensions via a web browser, or inside the IDE; this is for inside the IDE. If you choose the other, you can go here. Navigate to the Extensions part of vscode; you can do this a couple ways, but the most future-proof method will probably be hitting F1, and typing out ext until an option called Extensions: Install Extensions becomes available. Alternatives are CtrlShiftx and from the top menu bar, View->Extensions
  4. Next you're going to want the following extensions; these aren't 100% necessary, but I'll let you decide what to keep after you've tinkered some:
    • Ruby; extension author Peng Lv
    • ruby-rubocop; extension author misogi
    • ruby-linter; extension author Cody Hoover
  5. Inside your ruby script's directory, we're going to make a directory via command-line called .vscode and in there we'll but a file called launch.json where we're going to store some config options.
    • launch.json contents

{ "version": "0.2.0", "configurations": [ { "name": "Debug Local File", "type":"Ruby", "request": "launch", "cwd": "${workspaceRoot}", "program": "{workspaceRoot}/../script_name.rb", "args": [], "showDebuggerOutput": true } ] }

  1. Follow the instructions from the extension authors for manual gem installations. It's located here for now: https://github.com/rubyide/vscode-ruby#install-ruby-dependencies
  2. You'll probably want the ability to put breakpoints wherever you like; not having this option enabled can cause confusion. To do this, we'll go to the top menu bar and select File->Preferences->Settings (or Ctrl, ) and scroll until you reach the Debug section. Expand it and look for a field called "debug.allowBreakpointsEverywhere" -- select that field and click on the little pencil-looking icon and set it to true.

After doing all that fun stuff, you should be able to set breakpoints and debug in a menu similar to this for mid-2017 and a darker theme:enter image description here with all the fun stuff like your call stack, variable viewer, etc.

The biggest PITA is 1) installing the pre-reqs and 2) Remembering to configure the .vscode\launch.json file. Only #2 should add any baggage to future projects, and you can just copy a generic enough config like the one listed above. There's probably a more general config location, but I don't know off the top of my head.

查看更多
泛滥B
7楼-- · 2019-01-01 07:14

All other answers already give almost everything... Just a little addition.

If you want some more IDE-like debugger (non-CLI) and are not afraid of using Vim as editor, I suggest Vim Ruby Debugger plugin for it.

Its documentation is pretty straightforward, so follow the link and see. In short, it allows you to set breakpoint at current line in editor, view local variables in nifty window on pause, step over/into — almost all usual debugger features.

For me it was pretty enjoyable to use this vim debugger for debugging a Rails app, although rich logger abilities of Rails almost eliminates the need for it.

查看更多
登录 后发表回答