Where can I find an actively developed lint tool f

2019-01-21 06:27发布

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.

9条回答
放我归山
2楼-- · 2019-01-21 07:08

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-21 07:12
avdi@lazarus:~$ irb
>> a = 20
=> 20
>> b = 30
=> 30
>> puts c
NameError: undefined local variable or method `c' for main:Object
        from (irb):3
>>

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.

查看更多
Root(大扎)
4楼-- · 2019-01-21 07:13

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.

查看更多
闹够了就滚
5楼-- · 2019-01-21 07:14

nitpick might be what you're lookng for.

With this code:

class MyString < String
  def awesome
    self.gsub("e", "3").gsub("l", "1").uppercase
  end
end

puts MyString.new("leet").awesome

... it outputs:

$ nitpick misspelling.rb 
*** Nitpick had trouble loading "misspelling.rb":
    NoMethodError undefined method `uppercase' for "133t":MyString
Nothing to report boss! He's clean!
查看更多
该账号已被封号
6楼-- · 2019-01-21 07:17

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).

查看更多
冷血范
7楼-- · 2019-01-21 07:18

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.

  • NetBeans Ruby pack
  • Aptana RadRails
  • gVIM (with syntastic plugin by scrooloose)

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.

查看更多
登录 后发表回答