title: Groovy string interpolation with value only known at runtime
body: <p>I feel like I'm missing something with how Groovy handles strings. I realize that they're immutable, but what I would like to do is interpolate a value at runtime. I can't figure out how. Let me give a really simple example in Python (as "executable pseudo-code") to illustrate what I mean. Then I'll give what I've tried in Groovy.</p>
  
  <p><strong>Python</strong></p>
  
  <pre><code># string_sample.py 
  
  class MyClass(object):
  
      greeting = 'Hello, %s!'
  
      def __init__(self):
          object.__init__(self)
  
      def sayHello(self, name):
          print self.greeting % name
  
  
  if __name__ == '__main__':
      m = MyClass()
      m.sayHello('Mario')
  </code></pre>
  
  <p>The above prints: <em>Hello, Mario!</em></p>
  
  <p><strong>Groovy</strong></p>
  
  <pre><code>// string_sample.groovy
  
  class MyClass {
  
      def greeting = "Hello, ${name}!"
  
      MyClass() {
      }
  
      void sayHello(name) {
          println greeting
      }
  }
  
  m = new MyClass()
  m.sayHello('Mario')
  </code></pre>
  
  <p>The above Groovy script complains that <code>name</code> is unknown:</p>
  
  <p><code>Caught: groovy.lang.MissingPropertyException: No such property: name for class: MyClass</code></p>
  
  <p>I understand what's happening and why. I'm just not sure what to do about it. I realize that <code>String.format</code> can be used, which isn't so bad: </p>
  
  <pre><code>    String greeting = "Hello, %s!" 
  
      // Omitted...
  
      void sayHello(name) {
          println String.format(greeting, name)
      }
  </code></pre>
  
  <p>I'm just thinking that maybe there's a <em>groovier</em> way of doing it. Anyone know? Thanks!</p>