And*_*imm 1 ruby code-analysis
我正在分析一些代码,我正在寻找字符串文字,以检查我是否有任何重复.例如,如果我有
def test_foo
input_filename = "foo.txt"
# ...
end
def test_bar
input_filename = "bar.txt" # Fine
# ...
end
def test_baz
# Bad! Refactor it to a constant that's shared by test_foo and test_baz
input_filename = "foo.txt"
# ...
end
Run Code Online (Sandbox Code Playgroud)
我希望分析程序告诉["foo.txt", "bar.txt", "foo.txt"]我源代码中存在的内容.
我怎样才能做到这一点?
如果你安装ruby_parser或parsetree,你将能够做这样的事情(假设程序文本在text):
result = RubyParser.new.parse(text)
result.flatten.to_a.select {|elt| elt.is_a?(String)}
Run Code Online (Sandbox Code Playgroud)
(这显然可以更好,但它应该足以让你开始!)