在哪里可以找到针对 Ruby 积极开发的 lint 工具?
我编写的大部分代码都是用 Ruby 编写的,每隔一段时间,我就会犯一些拼写错误,但过一段时间后才会被发现。当我让我的脚本运行很长的任务,然后返回发现我有一个拼写错误时,这很烦人。
是否有针对 Ruby 积极开发的 lint 工具可以帮助我克服这个问题?是否可以在一个处理大量源文件(其中一些是动态加载的)的系统中使用它?
以此片段为例:
a = 20
b = 30
puts c
要赢得赏金,请向我展示一个工具,该工具将检测 c
变量是否未创建/未定义。
Most of the code I write is in Ruby, and every once in a while, I make some typo which only gets caught after a while. This is irritating when I have my scripts running long tasks, and return to find I had a typo.
Is there an actively developed lint tool for Ruby that could help me overcome this? Would it be possible to use it across a system that works with a lot of source files, some of them loaded dynamically?
Take this snippet as an example:
a = 20
b = 30
puts c
To win bounty, show me a tool that will detect the c
variable as not created/undefined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
[Plug] 如果您的项目位于公共 Github 存储库中,Caliper 可以在您的代码上运行后三个工具和其他工具每次你承诺的时候。 (免责声明:我从事 Caliper 工作)
ruby -c myfile.rb
will check for correct Ruby syntax.[Plug] If your project is in a public Github repository, Caliper can run the latter three tools and others on your code every time you commit. (Disclaimer: I work on Caliper)
您可以尝试Diamondback Ruby。它对 Ruby 代码进行静态类型检查,因此会责怪您使用未定义的变量。
虽然 DRuby 是一个正在进行的研究项目,但它已经非常适合小型、独立的 Ruby 脚本。目前,它无法“开箱即用”地分析 Ruby 标准库的大部分内容。目前,他们正在致力于编写 Ruby on Rails(请参阅他们的最新论文)。
You could give Diamondback Ruby a try. It does a static typecheck of Ruby code, and will thus blame you for using an undefined variable.
While DRuby is an ongoing research project, it already works quite well for small, self-contained Ruby scripts. Currently, it is unable to analyze much of the Ruby standard library “out-of-the-box”. Currently they are working toward typing Ruby on Rails (see their most recent papers).
RubyMine (http://www.jetbrains.com/ruby) 可以解决这个问题:
alt text http://img707.imageshack.us/img707/5688/31911448.png
下面的内容都不会完成 RubyMine 所做的所有分析。
其中每一个都能够识别语法错误,例如括号数量错误、定义过多、结尾、大括号等。但没有一个能够像 RubyMine 那样识别无效方法调用做。
原因如下:这很困难。
由于 Ruby 非常动态(并且可以轻松地动态生成类似“c”的方法),因此任何试图识别不存在的变量/方法的编辑器都需要加载整个环境的很大一部分和多个程序流路径不断测试以获得准确的“有效性”结果。这比在 Java 中困难得多,在 Java 中几乎所有编程都是静态的(至少当我放下那顶帽子时是这样)。
这种轻松动态生成方法的能力是社区如此高度重视测试的原因之一。我真的建议您也尝试进行测试。
RubyMine (http://www.jetbrains.com/ruby) does the trick:
alt text http://img707.imageshack.us/img707/5688/31911448.png
None of the below will do all the analysis that RubyMine does.
Each of these has the capacity to identify syntax errors such as wrong number of parentheses, too many defs, ends, braces, etc. But none will identify invalid method calls the way RubyMine does.
Here's why: it's difficult.
Since Ruby is extremely dynamic (and methods like 'c' could easily be generated on the fly), any editor that tries to identify non-existent variables/methods would need to have a large part of the entire evironment loaded and multiple program flow paths constantly tested in order to get accurate 'validity' results. This is much more difficult than in Java where almost all programming is static (at least it was when I dropped that hat).
This ability to easily generate methods on the fly is one of the reasons the community holds testing to such high esteem. I really do reccomend you try testing as well.
看看 RuboCop。它是一个基于 Ruby 样式指南 的 Ruby 代码样式检查器。它的维护非常活跃,并且支持所有主要的 Ruby 实现。它与 Ruby 1.9 和 2.0 配合良好,并且与 Emacs 集成良好。
Have a look at RuboCop. It is a Ruby code style checker based on the Ruby Style Guide. It's maintained pretty actively and supports all major Ruby implementations. It works well with Ruby 1.9 and 2.0 and has great Emacs integration.
是的。
Test::Unit
好吧,我知道你已经知道这一点,从某种意义上来说,这是一个没有帮助的答案,但你确实提出了鸭子类型的负面后果,那就是(此时)没有办法只编写比 Java 等可能需要的更多的测试。
因此,为了记录,请参阅 Ruby 标准库中的
Test::Unit
或其他测试框架之一。拥有可以运行和重新运行的单元测试是捕获错误的最佳方法,并且在像 Ruby 这样的动态语言中,您确实需要更多的单元测试(测试,而不是错误:-)...
Yes.
Test::Unit
Ok, I know you already know this and that in some sense this is a non-helpful answer, but you do bring up the negative consequence of duck typing, that there kind of is (at this time) no way around just writing more tests than something like Java might need.
So, for the record, see
Test::Unit
in the Ruby Standard Library or one of the other test frameworks.Having unit tests that you can run and rerun is the best way to catch errors, and you do need more of them (tests, not errors :-) in dynamic languages like Ruby...
nitpick 可能就是您正在寻找的。
使用此代码:
...它输出:
nitpick might be what you're lookng for.
With this code:
... it outputs:
尚未使用它,但听起来很有希望(当我测试过它时会更新)。
https://github.com/michaeledgar/laser
Ruby 代码的静态分析和样式 linter。
Have not used it yet, but sounds promising (will update when I've tested this).
https://github.com/michaeledgar/laser
Static analysis and style linter for Ruby code.
Pelusa 很好,但仅在 rubinius 中工作。不过,对于熟悉 RVM 的人来说,这不应该是一个问题。
Pelusa is nice, but is working in rubinius only. This shouln't be a proplem for people familar with RVM though.
就这样,该工具称为“IRB”。我能得到赏金吗?
我只是半开玩笑。我写第二个答案的目的是希望让大家明白,在 Ruby 中,如果您想知道某些内容是否已定义,您必须运行代码。
There ya go, the tool is called "IRB". Do I get the bounty?
I'm only half joking. I wrote this second answer to hopefully drive home the point that in Ruby, if you want to know that something is defined or not, you have to run the code.