59

I see { } are used for closures, and then I believe when a $ is put in front of braces, it is simply doing a variable substitution within a string. I can't find the documentation on how the $ works in the reference ... hard to search on it unfortunately, and the Groovy String documentation is lacking in introducing this. Can you please point me to the documentation and/or explain the "$" operator in Groovy -- how all it can be used? Does Grails extend it at all beyond Groovy?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ray
  • 5,885
  • 16
  • 61
  • 97

4 Answers4

64

In a GString (groovy string), any valid Groovy expression can be enclosed in the ${...} including method calls etc.

This is detailed in the following page.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40
  • Thanks for pointing that it's in the GString section. The String section used ${} in one of its examples and didn't introduce it. – Ray Aug 03 '11 at 17:34
  • 2
    I never knew one could do so much in a GString. – nburr Feb 26 '18 at 18:41
  • 4
    One precaution that some of us may miss, do use double quotes to make it work i.e 'Hello ${varSam}' will not work but "Hello ${varSam}" – old-monk May 09 '18 at 16:15
52

Grails does not extend the usage of $ beyond Groovy. Here are two practical usages of $

String Interpolation

Within a GString you can use $ without {} to evaluate a property path, e.g.

def date = new Date()
println "The time is $date.time"

If you want to evaluate an expression which is more complex than a property path, you must use ${}, e.g.

println "The time is ${new Date().getTime()}"

Dynamic Code Execution

Dynamically accessing a property

def prop = "time"
new Date()."$prop"

Dynamically invoking a method

def prop = "toString"
new Date()."$prop"()

As pointed out in the comments this is really just a special case of string interpolation, because the following is also valid

new Date().'toString'()
Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 1
    Isn't your 2nd usage just an example of the 1st? As you can call methods by their string name like `new Date().'toString'()`? So it's just the first usage case to build a String which is then used to access methods/properties? – tim_yates Aug 03 '11 at 10:09
  • 1
    @tim - you're right, I guess what I'm really showing are two practical usages of string interpolation rather than two usages of $. I'll try and make this clearer – Dónal Aug 03 '11 at 11:24
  • +1 from me :-) Sorry I was being slightly pedantic, as it's a good example of where `$` is really useful – tim_yates Aug 03 '11 at 11:32
  • Cool - thanks for showing an example of dynamically accessing a property and dynamically invoking method – Ray Aug 03 '11 at 17:55
  • I'd like to mark yours as an answer as well, or raise the points, but it is not allowing me to. Thanks for the grails clarification and examples. – Ray Aug 03 '11 at 18:03
  • if the string containing $variable, arrives from a select on the database, can it work? – andQlimax Apr 22 '21 at 08:38
11

$ is not an operator in Groovy. In string substitution it identifies variables within the string - there's no magic there. It's a common format used for inline variables in many template and programming languages.

All special Groovy operators are listed here: http://groovy-lang.org/operators.html

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
OverZealous
  • 39,252
  • 15
  • 98
  • 100
1

Work in side Jenkins File in pipeline enter image description here

#!/usr/bin/env groovy
node{
          stage ('print'){
             def DestPath="D\$\\"
             println("DestPath:${DestPath}")
          }
}
Bruno
  • 3,872
  • 4
  • 20
  • 37
Robert A
  • 337
  • 4
  • 3