I’m analyzing some code and I’m looking for string literals, to check if I have any duplicates. For example, if I have
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
I want the analysis program to tell me that ["foo.txt", "bar.txt", "foo.txt"] exist in my source code.
How can I do this?
If you install ruby_parser or parsetree, you’ll be able to do something like this (assuming that the program text is in
text):(This could obviously be nicer, but it should be enough to get you started!)