RubyMine 2017.3 Help

Debugging Your First Ruby Script

Finding out the origin of the problem

Let's run our script again. In the console, enter the following: first quotient = 10, second quotient = 1, third quotient = 2.

Oops... RubyMine reports a run-time error. It seems that we have to add a check of the discriminant value. Also, it would be nice to repeat the whole process more than once.

Surrounding code

To avoid running into the same problem again, let's add an if statement to check whether the discriminant is less that zero. To do that, select the statements next to discriminant calculation and then press Ctrl+Alt+T (Code | Surround with).

/help/img/idea/2017.3/rm_surround.png

Furthermore, let's add more checks - if the discriminant equals zero.

RubyMine creates a stub if construct, leaving you with the task of filling it with the proper contents.

Finally, select all the statements of the script and repeat surrounding - this time, with the While statement. You'll end up with the following code:

loop do p "insert first quotient" a = gets.to_f p "insert second quotient" b = gets.to_f p "insert third quotient" c = gets.to_f discriminant = b**2 - 4 * a * c if discriminant > 0 square_root = Math.sqrt(discriminant) x1 = ((-b + square_root) / (2 * a)) x2 = ((-b - square_root) / (2 * a)) p "D = #{discriminant}, x1 = #{x1}, x2 = #{x2}" elsif discriminant == 0 x = (-b / (2 * a)) p "D = #{discriminant}, x1 = x2 = #{x}" else p "D = #{discriminant}, no roots" end end

Let's take a closer look to see how the debugger can show your what your code is doing.

Debugging in detail

The Debug tool window shows dedicated panes for frames, variables, and watches, and the console, where all the input and output information is displayed. If you want the console to be always visible, you can drag it to one of the RubyMine window's edges.

We'll dig a little deeper into our code to find out what’s going wrong. We can use the RubyMine debugger to see exactly what’s happening in our code. To start debugging, you have to set some breakpoints first. To create breakpoints, just click in the left gutter:

/help/img/idea/2017.3/rm_breakpoints_created.png

Stepping

If you want to see what your code does line by line, there's no need to put a breakpoint on every line, you can step through your code.

Let's see what it looks like to step through our example program: click the /help/img/idea/2017.3/debug_resume.png button, go to the Console to ask for the value, and we can see that we hit our breakpoint.

We can use the stepping toolbar buttons to choose on which line we'd like to stop next.

/help/img/idea/2017.3/rm_stepping_toolbar.png

For example, click the Step Over button (/help/img/idea/2017.3/frames_step_over.png) and see the blue marker moving to the next line of code:

/help/img/idea/2017.3/rm_debugging1_step_over.png

If you want to concentrate on your own code, use the Step Into My Code button (/help/img/idea/2017.3/step_into_my_code.png) - thus you'll avoid stepping into library classes.

See the Stepping toolbar and Stepping Through the Program sections for details.

Watching

RubyMine allows you to watch any variable. Just click /help/img/idea/2017.3/new-watch.png on the toolbar of the Variables tab, and type the name of the variable you want to watch - let it be discriminant . Note that code completion is available here:

/help/img/idea/2017.3/rm_debugging1_watch_completion.png

At first, you see that discriminant equals nil - it means that the variable is not yet defined:

/help/img/idea/2017.3/rm_debugging1_watch_error.png

However, when the program execution continues to the scope that defines the variable, the watch gets the following view:

/help/img/idea/2017.3/rm_debugging1_watch_normal.png

See Adding, Editing and Removing Watches section for details.

Inline debugging

You may have noticed another RubyMine feature that makes it easy to see what your code is doing: the inline debugger. As soon as you hit any breakpoint, RubyMine shows you the value of many of your variables right in the editor:

/help/img/idea/2017.3/rm_debugging.png

This inline debugging feature is enabled by default. If you don't see the inline debugging values, please check that it's enabled using the settings icon /help/img/idea/2017.3/projectToolWindowSettingsIcon.png on the Debug Tool Window:

/help/img/idea/2017.3/rm_inline_debugging1.png

Evaluating expressions

Finally, you can evaluate any expression at any time. For example, if you want to see the value of a variable, click the /help/img/idea/2017.3/variables_evaluate_expr.png button:

/help/img/idea/2017.3/rm_debugging1_evaluate_expression.png

Then in the dialog box that opens, click Evaluate:

/help/img/idea/2017.3/rm_debugging1_evaluate_expression_action.png

Actually, you could see the same thing with a watch. With evaluate expression you can do things that you can't do with a watch: you can change things.

For example, if you enter the desired value of the first quotient, say, 30, and then continue stepping through your script, you will get the following:

/help/img/idea/2017.3/rm_debugging1_evaluate_expression_change_value.png

See the Evaluating Expressions section for details.

Summary

So, you've done it! Congrats! Let's repeat what you've done with the help of RubyMine:

  • Found out the origin of the problem
  • Set breakpoints
  • Stepped through your program
  • Created a watch
  • Evaluated an expression

See Also

Last modified: 15 September 2017