Groovy中的字符串连接似乎不方便

Zom*_*ies 0 groovy

我的目标是写下这个:

println "this should be 3: ($1+2)" //this is invalid groovy, it won't run
Run Code Online (Sandbox Code Playgroud)

然而这在红宝石中是有效的.有没有一种方法可以将语句放在字符串中,或​​者我必须使用完整的变量名称?我基本上是在寻找相当于Ruby的:

puts "this shoud be 3: #{1+2}" #this is valid ruby
Run Code Online (Sandbox Code Playgroud)

Dón*_*nal 11

这就是你需要的

println "this should be 3: ${1+2}"
Run Code Online (Sandbox Code Playgroud)

如果要评估的代码是变量名称或GPath表达式,则可以省略花括号,例如

def foo = "bar"
println "The value is $foo"
Run Code Online (Sandbox Code Playgroud)

但是如果你想要安全起见,总是把代码放在里面 ${}