Ruby Flay Gem – DRY code base
Tutorial will discuss Ruby Flay gem used to DRY up Ruby/Rails application. Flay gem enforces Best practices to be followed while coding with Ruby and Rails
Flay gem
Source: Flay on Github
When to Use? - To make your codebase more DRY!
Usage
1) Install gem by -
2) Process your files.
This command will process ruby/erb (.rb/.erb) files in current directory. You can also run as,
Above command will process all the files in the app/models directory.
3) Output
The flay command will give output -
Above output shows where the codebase has duplication with filename and line number to check for the same.
The gem scans the files for structural similarities. This is useful for making your codebase more DRY (Don’t Repeat Yourself)
How to Refactor?
When you check out the file and the line number pointed out by Flay gem you will realize the similarities and you would want to refactor it.
Listing down some of the techniques -
-
Refactor code pointed by Flay to a method.
-
Call the method from all the places pointed out by Flay.
-
Check whether you can create a module out of similar code. It would be useful in future as well.
Points to Remember
-
The Flay gives score after scanning which is an integer value.
-
Lower the Flay score, more DRY is your codebase
-
Try optimizing to decrease Flay (Badness) score
Integrating with VIM
Flay gem has inspired integration with editor such as emacs, textmate, vim. You can integrate with VIM by following instructions at Vim Flay on Github
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox