我正在尝试使用 ruby 读取 XML 格式的声纳报告。我写了以下代码:
class Resource
attr_accessor :file_name, :lines, :generated_lines, :ncloc, :generated_ncloc, :statements, :comment_lines, :commented_out_code_lines, :lines_to_cover, :uncovered_lines, :conditions_to_cover, :uncovered_conditions, :line_coverage, :branch_coverage, :coverage, :test_success_density, :test_failures, :skipped_tests, :test_errors, :num_of_tests, :lang, :qualifier
def to_s
"#{@name} #{@lines} #{@generated_lines} #{@ncloc} #{@generated_ncloc} #{@statements} #{@comment_lines} #{@commented_out_code_lines} #{@lines_to_cover} #{@uncovered_lines} #{@conditions_to_cover} #{@uncovered_conditions} #{@line_coverage} #{@branch_coverage} #{@coverage} #{@test_success_density} #{@test_failures} #{@skipped_tests} #{@test_errors}#{@num_of_tests} #{@lang} #{@qualifier}"
end
end
sonar.root.each_element do |node1| # resources
next if node1.name != "resources"
node1.each_element do |node2| #resource
resource = Resource.new
resource.lang = node2.text if node2.name == "lang"
resource.qualifier = node2.text if node2.name == "qualifier"
resource.name = node.text if node2.name == "name"
end
end
if
如您所见,无论我使用条件还是语句,这都占用了太多case
语句。在红宝石中有更简洁的方法吗?