Book Image

Instant RubyMine Assimilation

By : David L. Jones
Book Image

Instant RubyMine Assimilation

By: David L. Jones

Overview of this book

Ruby and Ruby on Rails applications can become very complex very quickly, with hundreds of different files to contend with during a typical day of development. With RubyMine, you can become much more productive and let the IDE take care of the menial tasks while you concentrate on the details and structure of your applications. Instant RubyMine Assimilation shows you how to integrate the RubyMine development environment into your everyday development process. With step-by-step instructions and practical examples, you will learn how to customize the environment precisely to suit your needs, debug and test your code, and get the most out of many other features of RubyMine. Starting with the installation of RubyMine on your preferred platform, this guide will walk you through creating your first program. You will be taken through everything from the development and testing process all the way to deploying your website for the world to enjoy. You will learn how to manage your project in RubyMine including integrating version control, testing, and debugging your projects. Furthermore, you will learn how to navigate complex projects, view database tables, and utilize the built-in console and deployment tools of RubyMine, all of which will help you become an expert developer This book will also teach you how to install and use Gems, change Ruby environments, and use code coverage and reports to enhance your testing. By the end of this book, you will be able to confidently deploy your Rails application to a server, all within the inviting environment that is RubyMine.
Table of Contents (7 chapters)

Refactoring and maintaining your tech (Become an expert)


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.

Getting ready

Open your HelloWorld project that we created earlier in RubyMine.

How to do it...

We will begin with how to refactor some code into a method:

  1. 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.

  2. 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:

  1. 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
  2. Click on the Refactor This menu item from Refactor. This will bring up a menu like the following:

  3. 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.

  4. Enter the method name draw_grid.

  5. Hit OK.

  6. 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
  7. The method call then looks like the following:

      draw_grid(grid, num_lines, pdf, size)
  8. Now lets try renaming a variable. Click on one of the variables named num_lines.

  9. Right-click and navigate to Refactor | Rename.

  10. Begin typing a new name for this variable, number_of_lines.

  11. 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.

There's more…

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.