Search code examples
groovyscopeintrospection

What's the Groovy equivalent to the no-arguments version of Python's dir()?


In Python, at runtime, you can get a list of the variable names currently in scope with dir(). Is there a way to do the same thing in Groovy?

dir(someobject) can also be used to inspect an object, but that's not what this question is about. That functionality is already described here.


Solution

  • Within a groovy script, you can inspect the binding:

    binding.variables.each{  
      println it.key 
      println it.value  
    } 
    

    There is no direct equivalent to Python's dir. However, there is at least one technique you can use to find which variables are in scope.