2011-05-18 20 views
4

如何在Ruby中檢測未使用的參數?是否有任何源代碼分析工具在Ruby中檢測到未使用的參數?

選擇我所知道的包括

  • 作爲嚴謹與TDD。
  • Heckle(由於ParseTree問題,目前僅適用於Ruby 1.8)
  • 使用IDE(如RubyMine)檢測未使用的參數或自動執行重構。

但是,是否有任何源代碼分析工具或警告選項允許您檢測未使用的參數?

背景:我在做一些重構。我從(代碼稍有簡化)改爲:

# Not within the Foo class (therefore can't be as easily accessed by unit testing) 
# and in addition, the name of the configuration file is hard-wired 
def parse_configuration 
    raw_configuration = YAML.load_file("configuration.yml") 
    # Do stuff with raw_configuration to produce configuration_options_for_foo 
    return configuration_options_for_foo 
end 

if __FILE__ == $0 
    configuration_options_for_foo = parse_configuration 
    foo = Foo.new(configuration_options_for_foo) 
end 

class Foo 
    # Now unit tests can call Foo.new_using_yaml("configuration.yml") 
    # or use "test_configuration.yml" 
    def self.new_using_yaml(yaml_filename) 
    # Where I went wrong, forgetting to replace "configuration.yml" with yaml_filename 
    raw_configuration = YAML.load_file("configuration.yml") 
    # Do stuff with raw_configuration to produce configuration_options_for_foo 
    new(configuration_options_for_foo) 
    end 
end 

if __FILE__ == $0 
    foo = Foo.new_using_yaml("configuration.yml") 
end 

回答

0

Ruby-lint可以檢測未使用的參數。

RuboCop有just added(2014年4月)使用警報「UnusedMethodArgument」檢測未使用的參數。

相關問題