In both Python and Ruby you can add methods just to an object ( instead of all instances of a class ). The great thing is, you can do the same in Groovy. Here’s how :
def a = new File(".")
a.metaClass.bla = {
println File.separator;
}
a.bla() // this will print \ or / , depending on your operating system
bla isn’t a suggestive name, I’m sure you agree. But, this illustrates the point. If for example, you instantiate a second object, and try to call the same method on it, like in the following example:
def b = new File("..")
b.bla() // this will throw a MissingMethodException
You can notice that the second object does not share the bla method.
I’m sure you guys can put this to good use !