【问题标题】:Do any source code analysis tools detect unused parameters in Ruby?是否有任何源代码分析工具检测 Ruby 中未使用的参数?
【发布时间】:2011-05-18 01:45:14
【问题描述】:

如何检测 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

【问题讨论】:

    标签: ruby code-analysis


    【解决方案1】:

    我认为 Laser 可以做到这一点。

    这是相当的 alpha-y,但似乎可以做你想做的事。

    http://github.com/michaeledgar/laser

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        Ruby-lint 可以检测到未使用的参数。

        RuboCop 使用 cop“UnusedMethodArgument”检测未使用参数的 just added(2014 年 4 月)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多