1

我最近一直在玩flog,它是一个非常好的工具,可以为 ruby​​ 应用程序生成代码复杂性报告。作为 flog在项目代码库上运行的结果,您将获得类似于以下内容的输出:

  1272.3: flog total
     7.3: flog/method average

    62.2: MyClass#foobar lib/myclass#foobar:123
    ... more similar lines ...

上面的示例提供了一个方法的分数,并在定义该方法的源代码中引用了确切的行号。这可以是常规实例/类方法或任何其他“动态”方法,例如。耙任务等。

因此,目标是从源文件中提取一段代码(很可能是一个方法),该代码段以 flog 输出中定义的行号开头。然后可以在某些 Web UI 中使用该片段来显示各种代码指标(基于其他工具,如flay)和/或存储在数据库中。据我所知,这个任务涉及将 ruby​​ 代码解析为 AST,然后通过树找到相应的起始行并计算出结束行号。我已经用这个库做了一些实验 - https://github.com/whitequark/parser,大部分时间都可以工作,但要获得正确的结果有点棘手。

是否有任何其他解决方案可以从用 ruby​​ 编写的源文件中快速提取方法代码?

4

1 回答 1

4

您可以实现按名称或行号查找方法的功能。

此示例代码显示如何按名称查找方法代码。它很脏,但它可以工作(在 Ruby 2.1.2 上)。这个使用parsergem install parser)。

# method_finder.rb
require 'parser'
require 'parser/current'

class MethodFinder
  def initialize(filename)
@ast = parse(filename)
  end

  def find(method_name)
recursive_search_ast(@ast, method_name)
return @method_source
  end

  private
  def parse(filename)
Parser::CurrentRuby.parse(File.open(filename, "r").read)
  end

  def recursive_search_ast(ast, method_name)
ast.children.each do |child|
  if child.class.to_s == "Parser::AST::Node"
    if (child.type.to_s == "def" or child.type.to_s == "defs") and (child.children[0].to_s == method_name or child.children[1].to_s == method_name)
      @method_source = child.loc.expression.source
    else
      recursive_search_ast(child, method_name)
    end
  end
end
  end
end

你可以MethodFinder像下面这样使用。

mf = MethodFinder.new("./method_finder.rb")
puts mf.find("find")

=> def find(method_name)
=>     recursive_search_ast(@ast, method_name)
=>     return @method_source
=>   end
于 2014-08-15T06:48:28.000 回答