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:25

There is many debuggers with different features, based on which you make choice. My priorities was satisfied with pry-moves which was:

  • quickly comprehensible info on how to use
  • intuitive steps (like easy stepping into blocks)
  • "stepping back" (pry-moves partially satisfies the need)
查看更多
墨雨无痕
3楼-- · 2019-01-01 07:26
  1. Print out the variables whenever possible. (This is called printf debugging) You can do this by running

    STDERR.puts x.inspect
    

    or

    STDERR.puts "Variable x is #{x.inspect}"
    

    If you want to make this easier to type, then you may want to use the exemplor gem.

  2. Turn warnings on. If you're running ruby then run it with the -w switch (eg ruby -w script.rb). If you're running it from irb, and you're using a version of ruby prior to 1.9.2, type $VERBOSE = true at the start of your session. If you misspell an instance variable, once warnings are on you'll get

    warning: instance variable @valeus not initialized

  3. Understand the concept of a binary chop (the following quote is from Practices of an Agile Developer)

    Divide the problem space in half, and see which half contains the problem. Then divide that half in half again, and repeat.

  4. If you're successful with a binary chop, you may find that there's a single line that doesn't do what you expect it to do. For example

    [1, 2, 3].include?([1,2])
    

    gives a value of false, even though you'd think it'd return true. In that case, you may want to look at the documentation. Web sites for documentation include ruby-doc.org, or APIdock. In the latter case, you'd type include? next to the magnifying glass near the top right corner, choose the include? which has Array underneath it (if you don't know what class [1, 2, 3] is, type [1, 2, 3].class in irb), and you get to include? (Array), which describes what it does.

    However, if the documentation doesn't help, you're more likely to get a good answer if you can ask a question on how a specific line isn't doing what it should, rather than why an entire script isn't doing what it should.

查看更多
怪性笑人.
4楼-- · 2019-01-01 07:32
  1. In Ruby:

    ruby -rdebug myscript.rb 
    

    then,

    • b <line>: put break-point
    • and n(ext) or s(tep) and c(ontinue)
    • p(uts) for display

    (like perl debug)

  2. In Rails: Launch the server with

    script/server --debugger
    

    and add debugger in the code.

查看更多
余生无你
5楼-- · 2019-01-01 07:32
  1. You can print your variables out along the way
  2. Turn on the -w (warnings) flag
  3. Use a tool such as ruby-debug
查看更多
千与千寻千般痛.
6楼-- · 2019-01-01 07:33

If you are using RubyMine, debugging ruby scripts is simple and straightforward.

Suppose you have a Ruby script hello_world.rb

1. Set breakpoints

Set a breakpoint at line 6 as below.

enter image description here

2. Start debugging

Now you can just start the debugger to run the script:

enter image description here

enter image description here

3. Inspect variables, etc.

Then when the execution hits a breakpoint, you'll be able to inspect variables, etc.

enter image description here

Further information for your reference

  1. If you would like to use RubyMine to do remote debugging, you can do so.
  2. If you would like to use RubyMine to remote debug rails running inside a docker, it is also straightforward.
查看更多
登录 后发表回答