Loading...

November 30, 2009

Groovy Goodness: Use a Category and Get a Return Value

With the use() method in Groovy we execute a closure in the scope of a Category or list of Categories. The last statement of the closure is the return value for the use() method. So we can get a result from the use() method and use it in our program.

class StringCategory {
    static String groovy(String self) {
        self + ' Yeah man.'
    }
}

def returnedValue = use(StringCategory) {
    def s = 'A simple String.'
    s.groovy()
}

assert 'A simple String. Yeah man.' == returnedValue