【发布时间】:2020-04-06 14:23:47
【问题描述】:
非常基本的问题,但我找不到答案:
我在文件g.groovy 中有以下代码,它在打印输出中起作用:
#! /usr/env/groovy
def matchFiles = { match ->
new File(".").eachFile() {
if (it.name =~ match) {
println it
}
}
}
matchFiles('.groovy') 将./g.groovy 打印到屏幕上。
但我想在变量中捕获闭包的输出并在其他地方使用它,例如
def fileMatches = matchFiles('.groovy')
但无法弄清楚。
尝试将 println it 更改为 return it 然后运行
def fileMatches = matchFiles('.groovy')
fileMatches.println { it }
但这会打印出类似g$_run_closure2@4b168fa9
非常感谢任何帮助,对于任何不正确的命名法,Groovy 非常新,抱歉
【问题讨论】: