Hello and happy Monday!
I first wanted to thank the various people who have showed support for my TDD series last week, I sincerely appreciate the re-tweets and favorites!
Over the weekend, while perusing the web I stumbled upon Rubocop. Running Rubocop looks a lot like running Rspec or Test::Unit in our terminal. Instead of running our specs though, Rubocop will generate ‘cops’ alerting us to various style-guide offenses, the gem even has an auto correct feature in experimentation at the moment. My thoughts in two words or less about Rubocop, pretty sweet! Lets take a look at how to use Rubocop as I attempt to refactor lunch_panoply and see if we can’t clean up some of the rough edges.
The setup is extremely easy, all we do is type
gem install rubocop from our command-line, or if you’re using Bundler you can add
gem 'rubocop' to your Gemfile, and install it by typing
bundle install in your terminal.
In order to run Rubocop on a specific file, we can just pass the path to file after the
rubocop command like so:
Thats all there is to it! To run Rubocop, we can think of how we run Zeus or Rspec in our terminal. From our application’s root in the terminal we type
rubocop to find out what our offenses will be.
Running the command above generates offenses.
And The… Law Won
Looks like Rubocop had a field day with my
venue_spec file. Lets get on top of addressing a few of these. Rubocop has 3 different types of ‘cops’. I think of cops as specs, but instead of creating them myself as I test-drive, they’re already built into the gem and alert us to three things about our code.
Style: Style cops check for stylistics problems in our code, nearly all of these are based on the Ruby Style Guide.
Lint: Lint cops check for bad practices and errors in our code.
Rails: Rails cops are specific to the Rails framework. These are the only cops not used by default.
We can single out Lint cops when desiring to discover offenses by passing in an
-l argument when entering the
rubocop command like
rubocop -l spec/models/venue_spec.rb
Rails cops aren’t used by default when entering the
rubocop command, in order to add them to the party, we must specifically request them like so:
rubocop -R models/model_name.rb
Rubocop in Action
Now that we have some of the basics covered lets take this out for a spin.
230 offenses detected Yuck! As we can see from the image above, all of these offenses are due to the
style cops not aren’t happy about tabs being used instead of spaces. I have my sublime text 2 formatted to spaces already as far as I know, so I’m not sure as to why we’re generating fake offenses. Luckily we can run the same command again with our
Lint argument to gain a clear picture of the error-related offenses only. Typing
rubocop -l spec/models/menu_item_spec.rb returns the following output:
My first impression of this gem is thumbs up! There are a few things I’d like to look into about reducing or eliminating false offenses being generated on the style front, but overall Rubocop is an extremely easy to setup, and quick to implement way to deoderize your code base.
It really makes my day when I run across other developer’s creations, especially ones that aid with my goal for me of becoming competent enough for employment with a company that values the ongoing process of learning. Keeping our code neat and tidy, is one of many things that I am hoping will allow me to appear desirable as I look for entry-level work.
If you find yourself desiring to improve the way your code appears to others, take some time and check out Bozhidar’s Github page.
Categories: Ruby on rails