如何在Ruby中检测未使用的参数?
我知道的选项包括
但是,是否有任何源代码分析工具或警告选项可以检测未使用的参数?
背景:我正在做一些重构。我改变了(代码稍微简化):
# 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 :(得分:1)
答案 1 :(得分:0)
Ruby-lint可以检测到未使用的参数。
RuboCop已使用警察" UnusedMethodArgument" just added(2014年4月)检测未使用的参数。
答案 2 :(得分:0)