有没有办法将所有定义的变量传递给 GroovyShell?

发布于 2024-08-06 00:33:00 字数 220 浏览 0 评论 0原文

现在,当我尝试在 Groovy 中评估一段代码时,我必须做这样的事情:

new GroovyShell(new Binding([var1:var1])).evaluate(line)

当您定义了很多变量时,这可能会非常令人讨厌。有更好的方法吗?是否有类似Python的locals之类的东西,或者类似的东西可以列出所有声明的变量?

Right now, when I'm trying to eval a piece of code in Groovy, I have to do something like this :

new GroovyShell(new Binding([var1:var1])).evaluate(line)

This can be pretty nasty when you have a lot of variables defined. Is there a better way of doing this? Is there something like Python's locals, or something similar that lists all the declared variables?

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

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

发布评论

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

评论(1

花开浅夏 2024-08-13 00:33:00

我还没有测试过这个...但它可能有效:

new GroovyShell(this.binding).evaluate(line)

或者这样:

new GroovyShell(new Binding(this.binding.variables)).evaluate(line)

I haven't tested this... but it may work:

new GroovyShell(this.binding).evaluate(line)

or this:

new GroovyShell(new Binding(this.binding.variables)).evaluate(line)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文