未加星标

William Denton: Rubocop

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

I hack a fair bit of Ruby, usually in a pretty plain way to solve a text-based problem; I can write scripts to do what I need, because my problems aren’t too large, but what I write isn’t elegant. Just recently I started using Rubocop in Emacs, and wow, what a great tool! It’s making the code better and me a better coder.

Rubocop is written by Bozhidar Batsov , who also made rubocop-emacs to integrate it into Emacs. I’ve had that running for a while and found it useful for pointing out unused variables and things like that, but otherwise it didn’t seem to do a lot. That’s because I had it turned on but wasn’t actually using it.

Here’s a short Ruby script:

#!/usr/bin/env ruby puts 'This is an example.' x = 5 y = ["foo", "bar", "baz"] if 2 > 1 puts "Yes, 2 > 1" else end

What it looks like in my Emacs with rubocop-mode on:


William Denton: Rubocop
Before.

The lines in red are highlighted because there’s a problem: in this case, the variables aren’t reused so, as the script stands, they could be removed without affecting anything.

Now, if I run rubocop on this script at the command line, it tells me helpful things about making the script better. If I run M-x rubocop-check-current-file in Emacs it tells me the same helpful things. But if I run M-x rubocop-autocorrect-current-file it fixes the problems .

Here’s the output:

-*- mode: compilation; default-directory: "~/" -*- Compilation started at Thu Oct 13 20:28:30 rubocop -a --format emacs /home/wtd/rubocop.rb /home/wtd/rubocop.rb:3:6: C: [Corrected] Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. /home/wtd/rubocop.rb:5:1: W: Useless assignment to variable - `x`. /home/wtd/rubocop.rb:7:1: W: Useless assignment to variable - `y`. /home/wtd/rubocop.rb:7:5: C: [Corrected] Use `%w` or `%W` for an array of words. /home/wtd/rubocop.rb:9:1: C: [Corrected] Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`. /home/wtd/rubocop.rb:11:1: C: [Corrected] Redundant `else`-clause. Compilation exited abnormally with code 1 at Thu Oct 13 20:28:30
William Denton: Rubocop
During.

(I can hit Enter on any of those lines and jump to that line in the code.)

All of the corrections are corrected and the warnings are left alone. The code is changed to this:

#!/usr/bin/env ruby puts "This is an example." x = 5 y = %w(foo bar baz) puts "Yes, 2 > 1" if 2 > 1


William Denton: Rubocop
After. Magic! It’s up to me to decide what to do with x and y, but all the style problems are fixed. That’s just a tiny example. Rubocop does a lot more, and I’ve been going through scripts changing CGI::parse to CGI.parse or foo["bar"] to foo[:bar] and all sorts of other small things that make them more readable and consistent.

The one thing I didn’t like about Rubocop was its preference for ‘single’ quotes over “double” quotes, so I put this in a new ~/.rubocop.yml file to tell it how I like things:

Style/StringLiterals: EnforcedStyle: double_quotes

Bozhidar Batsov: thank you.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: RubyCG
分页:12
转载请注明
本文标题:William Denton: Rubocop
本站链接:http://www.codesec.net/view/482702.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(28)