有源代码分析工具可以检测 Ruby 中未使用的参数吗?

发布于 2024-11-07 17:38:19 字数 1296 浏览 0 评论 0 原文

如何检测 Ruby 中未使用的参数?

我知道的选项包括

  • 严格 TDD。
  • Heckle(由于 ParseTree 问题,目前仅适用于 Ruby 1.8)
  • 使用 RubyMine 等 IDE 来检测未使用的参数或自动进行重构。

但是是否有源代码分析工具或警告选项允许您检测未使用的参数?

背景:我正在做一些重构。我从(代码稍微简化)更改

# 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

How can I detect unused parameters in Ruby?

Options I'm aware of include

  • Being rigorous with TDD.
  • Heckle (currently works only with Ruby 1.8 due to ParseTree issues)
  • Using an IDE such as RubyMine to detect unused parameters or automate the refactoring.

But do any source code analysis tools or warning options allow you to detect unused parameters?

Background: I was doing some refactoring. I changed from (code slightly simplified):

# 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

to

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

守不住的情 2024-11-14 17:38:19

我认为激光可以做到这一点。

这是相当阿尔法-y,但似乎做你想做的事。

http://github.com/michaeledgar/laser

I think Laser does this.

It's pretty alpha-y, but seems to do what you want.

http://github.com/michaeledgar/laser

喜你已久 2024-11-14 17:38:19

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

RuboCop 刚刚添加(2014 年 4 月)使用 cop 检测未使用的参数“未使用的方法参数”。

Ruby-lint can detect an unused parameter.

RuboCop has just added (April 2014) detection of unused parameters with the cop "UnusedMethodArgument".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文