是否可以将字符串计算为 groovy 中的变量?

发布于 2024-12-11 05:36:50 字数 331 浏览 1 评论 0原文

我正在玩 grails 和 groovy。我想知道是否可以做这样的事情。

def inbuiltReqAttributes = ['actionName','actionUri','controllerName','controllerUri']
inbuiltReqAttributes.each() { print " ${it} = ? " }; 

我会在里面放什么?让groovy将当前迭代器值作为变量进行评估,例如,长期这样做

print " actionName = $actionName "

谢谢

I am playing with grails and groovy. I wondered if its possible to do something like this.

def inbuiltReqAttributes = ['actionName','actionUri','controllerName','controllerUri']
inbuiltReqAttributes.each() { print " ${it} = ? " }; 

what would i put in the ? to get groovy to evaluate the current iterator value as a variable e.g. to do it the long way

print " actionName = $actionName "

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2024-12-18 05:36:50

我相信我的头脑中,这应该有效:

print " ${it} = ${this[ it ]}"

或者:

print " ${it} = ${getProperty( it )}"

但我不在计算机上 100% 验证这个 atm...

I believe off the top of my head, this should work:

print " ${it} = ${this[ it ]}"

Or:

print " ${it} = ${getProperty( it )}"

But i'm not at a computer to 100% verify this atm...

梦里兽 2024-12-18 05:36:50

试试这个:

inbuiltReqAttributes.each() {
  evaluate("value = ${it}")
  print "$it = $value"
}

Try this:

inbuiltReqAttributes.each() {
  evaluate("value = ${it}")
  print "$it = $value"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文