Archive | Ruby RSS for this section

More guard-rubocop (autocorrect and configure)

An update from my last post, and a couple more tips and questions for guard-rubocop:

In the directory where you install guard-rubocop, you also want to use rvm to specify a version of ruby and specific gemset for that directory. DO THIS BEFORE INSTALLING GUARD-RUBOCOP. So, go to whatever directory you want guard-rubocop installed in, and type:

echo '2.1.2' > .ruby-version

and

echo 'name_of_directory' > .ruby-gemset

Now to make these files take effect, change out of this directory and back in, like so:

cd .

This will generate wrappers for the direcotry and, you can now install guard-rubocop as per my last post, and it should fix the problem I faced before of needing to initialize guard every time i wanted to run it.

A few things I learned:
You can specify a directory or file for rubocop to inspect, instead of all files in that directory. Just run:

rubocop path/to/file.rb

Rubocop can also autocorrect some offenses like so:

rubocop --auto-correct
or
rubocop -a

In addition, I finally figured out how to configure guard-rubocop, so I can specify which conventions (called cops) I want rubocop to notify me about, and how to modify some of the rules. After several failed attempts at following the guide at https://github.com/bbatsov/rubocop, I finally found the sought after /config/default.yml file. Alright, so here is how to configure guard-rubocop:

1) First follow all steps in my previous blog post and the beginning of this post.
2) locate the config/default.yml file. I did this by opening terminal and searching from my home directory by typing

$ find ~ -name "default.yml"

This gave me the following list:

These are all the locations where I installed guard-rubocop, and where the default.yml file is. I suppose you could use any of these files, because I assume they are all the same. I chose the one in the directory ruby-2.1.2, rather than any of the ruby-2.1.2@some_other_directory, so I copied the full path to the file on my clipboard (you need to copy your own path to that file, not mine):

/Users/glassbead/.rvm/gems/ruby-2.1.2/gems/rubocop-0.23.0/config/default.yml

If your terminal did not return any results for the search, go back and try installing guard-rubocop again. Phew!

3) Ok, now use the terminal to go to the directory where you want to install the modifications to guard-rubocop, and create a .rubocop.yml file. My preferred method of this to to type

nano .rubocop.yml

This will create the file, and open a built in text editor where you can add text to the file. Add the following text (after the inherit_from: part, you can paste the path you copied before):

you can then quit the text editor by typing ctrl+x, pressing ‘y’ to save the changes, and pressing enter. You’ve now created the .rubocop.yml file, which list the modifications, or deviations from the default. Any lines added in this file will override the ones in default.yml.
The cops in the example above do the following:

Style/LineLength:
Max: 100

This changes the maximum line length that rubocop allows. So I can now have lines up to 100 characters, instead of 80.

Style/StringLiterals:
Enabled: false

This stops rubocop from complaining about double quotes (which it does if you don’t use string interpolation or special characters).

Style/FormatString:
EnforcedStyle: percent
SupportedStyles:
– format
– sprintf
– percent

This changes the prefered style of string formatting to ‘percent’ (it is format by ‘default’). Before I included this, rubocop would complain whenever I used string formatting like this:

Now it doesn’t complain (although I imagine it will now complain when you use sprintf or format)

You can peruse the default.yml file here to find other modification you might want to try. You can always go back to the .rubocop.yml file by typing nano .rubocop.yml and editing the cops listed.

Happy policing!

A question I still have is: What is “guard” vs. what is “rubocop”? I’m still new to the whole gem thing…

Thanks to Bill Shannon and Jason Noble for their blog posts about this topic as well.

Advertisements

Post[0]

Welcome to my blog! My name is Aaron Apollo Glasenapp. I’m an aspiring ruby on rails web developer, and a hard core recreational mathematician. I’ll be posting anything from fun math/computer science problems to bug fixes to who knows what else.

I’m currently taking the Da Vinci Coders rails boot camp, and will be posting many things related to the class, including solutions to some problems I come across, and perhaps problems that I have not yet solved.

To start us off, here’s a fun program I wrote using the javascript and processing.js platform on khanacademy.org. Play around and have fun!

2-D Cellular Automata

2-D Cellular Automata