Once we have written our code, good developers continue to refine that code using techniques called refactoring. RubyMine has some excellent tools to help us with that task. In addition, there are database, method, and class viewers and hierarchies. These tools can also help us in locating duplicate code and variables using advanced searching techniques.
We will begin with how to refactor some code into a method:
Open the
helloworld.rb
file from the project window on the left. We will need to fix a bug in the code first and then we will refactor some of the code into a method that we can reuse later.On the line 24 change the following:
51.times do |idx|
Change the preceding line to the following:
num_lines.times do |idx|
Now we can select a portion of code that lends itself to being refactored to improve our tech:
Select the following entire block of code using your mouse:
pdf.stroke do num_lines.times do |idx| pdf.line([0, idx*grid], [size, idx*grid]) pdf.line([idx*grid, 0], [idx*grid, size]) end end
Click on the Refactor This menu item from Refactor. This will bring up a menu like the following:
Select #6 Method… and a window will be shown that will let us choose the method name and order of parameters that will be passed to the method, as shown in the following screenshot. RubyMine automatically determines which parameters are needed to complete the code.
Enter the method name
draw_grid
.Hit OK.
RubyMine will now create a method with the selected code and insert a call to the new method
draw_grid
. The method is placed at the top of the code and should look like the following code:def draw_grid(grid, num_lines, pdf, size) pdf.stroke do num_lines.times do |idx| pdf.line([0, idx*grid], [size, idx*grid]) pdf.line([idx*grid, 0], [idx*grid, size]) end end end
The method call then looks like the following:
draw_grid(grid, num_lines, pdf, size)
Now lets try renaming a variable. Click on one of the variables named
num_lines
.Right-click and navigate to Refactor | Rename.
Begin typing a new name for this variable,
number_of_lines
.Notice that the variables throughout the file are changed as you type.
If that doesn't get your Borg juices flowing, then you are just not worth assimilating.
Another form of refactoring is just the reverse of the extract method which is called inlining. RubyMine will basically remove the method and put the code back inline with the rest of the code, eliminating a method that might only be used once, for instance.
In addition, RubyMine allows you to create variables from hard-coded values, extract constants, modules, classes—just about anything that you can imagine.
RubyMine can also show us some common errors that can creep into our projects by running an inspection on our codebase and analyzing against some known code style issues. Not all of the issues are errors, but merely coding style items that are brought to our attention.
Select the Inspect Code menu item from Code and the code will be analyzed and a panel opened similar to the one in the following screenshot. Notice that it finds double-quoted strings where they are not needed and some semicolons at the end of statements that are unnecessary. Ruby has no problem with these issues, but having semicolons are based on some old "tech" that has leaked into our assimilation process that we need to surgically remove. This can be done by simply removing the semicolons and rerunning the code inspection. This is very useful when looking at larger code bases like an entire Rails application.