Tiago Fernandez
Tiago Fernandez

Reputation: 1473

Binding variables access in Groovy script's defined classes

Given a regular Groovy script, is there a way to access its binding variables from classes defined inside the script itself?

The following snippet

class Example {
  def printBindings() {
    for (var in binding.variables) {
      println "$var.key - $var.value"
    }
  }
}
new Example().printBindings()

fails with the exception below:

groovy.lang.MissingPropertyException: No such property: binding for class: Example

Upvotes: 3

Views: 3421

Answers (1)

tim_yates
tim_yates

Reputation: 171054

Not that I can find without passing the script through to the method:

class Example {
  def printBindings( container ) {
    for (var in container.binding.variables) {
      println "$var.key - $var.value"
    }
  }
}
new Example().printBindings( this )

Upvotes: 1

Related Questions