In the following Groovy code snipped to add a fileAsString method to the String class, could someone explain what exactly ‘this’ refers to. I thought it was the object on which the fileAsString method is invoked, but apparently that’s actually what delegate refers to.
String.metaClass.fileAsString = { this.class.getResourceAsStream(delegate).getText() }
Thanks, Don
The newly defined method is a closure, so ‘this’ will have the same meaning as it does when the method is defined. Usually ‘this’ will refer to the object that defined the method, like below: