In Ruby, I can add instance variables to the class by opening it and doing something like this:
class Whatever def add_x @x = 20 end end
and this will add me an instance variable named x. How can I do the same in Groovy?
You can use the Groovy metaclass:
class Foo { String bar } f = new Foo(bar:"one") f.metaClass.spam = "two" f.spam == "two" // returns true f.spam = "eggs" // Change property value f.spam == "eggs" //returns true
Source: https://habr.com/ru/post/1714257/More articles:Enter YUI button in DataTable - javascriptAccess javascript variables from a call zone - javascriptHaskell and Quadratics - mathIs it a good practice to make multiple separate database calls from an ASP.NET MVC web application? - asp.net-mvcXSLT: how can I call a template when there is no input file? - xsltException checked or thrown - javaHelp in defining a programming language / script - programming-languages | fooobar.comTest module in Python without using exceptions - pythonHow to stop JavaScript MVC RedirectToRouteResult? - javascriptHow to remove Visual C ++ "Expand Menu" Arrow? - visual-c ++All Articles