How can I compare two lists in Groovy
I’d just use the arithmetic operators, I think it’s much more obvious what’s going on: def a = [“foo”, “bar”, “baz”, “baz”] def b = [“foo”, “qux”] assert [“bar”, “baz”, “baz”, “qux”] == ((a – b) + (b – a))
I’d just use the arithmetic operators, I think it’s much more obvious what’s going on: def a = [“foo”, “bar”, “baz”, “baz”] def b = [“foo”, “qux”] assert [“bar”, “baz”, “baz”, “qux”] == ((a – b) + (b – a))
Solution: org.codehaus.groovy.runtime.StackTraceUtils.sanitize(new Exception()).printStackTrace() Original answer: A Google search returns the following information: Apparently, there is a method in org.codehaus.groovy.runtime.StackTraceUtils called printSanitizedStackTrace. There isn’t much documentation for the method, though there is a method called sanitize which is described as remove all apparently groovy-internal trace entries from the exception instance This modifies the original instance and … Read more
An assertion is similar to an if, it verifies the expression you provide: if the expression is true it continues the execution to the next statement (and prints nothing), if the expression is false, it raises an AssertionError. You can customize the error message providing a message separated by a colon like this: assert 4 … Read more
A lot of the comments for duck typing don’t really substantiate the claims. Not “having to worry” about a type is not sustainable for maintenance or making an application extendable. I’ve really had a good opportunity to see Grails in action over my last contract and its quite funny to watch really. Everyone is happy … Read more
Same as in Java: def str2 = str1.replaceAll(‘\\)’,’ ‘) You have to escape the backslash (with another backslash).
The trick was to use a list: println([‘ls’, ‘/tmp/folder with spaces’].execute().text)
I haven’t yet found a good approach for excluding the read-only properties (i.e., metaClass, class), but if you want to set the value of all properties in the Foo instance that are also in the Foo2 instance you could do the following. class Foo { def feck = “fe” def arse = “ar” def drink … Read more
These are plain methods but they follow quite a specific pattern – they take a Closure as their last argument. A Closure is a piece of functionality that you can pass around and call when applicable. For example, method eachWithIndex might look like this (roughly): void eachWithIndex(Closure operation) { for (int i = 0; this.hasNext(); … Read more
You should put the \n first. That way your newline will be written before your text: f.append(‘\nhello again!’)