Add a variable to a class instance at runtime: Difference between revisions

Content added Content deleted
(update task description to indicate that the instance being modified must be an "object" instance and must not itself be a class)
Line 1: Line 1:
{{task|Object oriented}}
{{task|Object oriented}}
Demonstrate how to dynamically add variables to a class instance at runtime.
Demonstrate how to dynamically add variables to an object (a class instance) at runtime.


This is useful when the methods/variables of an instance are based on a data file that isn't available until runtime. Hal Fulton gives an example of creating an OO CSV parser at [http://www.devsource.com/article2/0,1759,1928562,00.asp An Exercise in Metaprogramming with Ruby]. This is referred to as "monkeypatching" by Pythonistas and some others.
This is useful when the methods/variables of an instance are based on a data file that isn't available until runtime. Hal Fulton gives an example of creating an OO CSV parser at [http://www.devsource.com/article2/0,1759,1928562,00.asp An Exercise in Metaprogramming with Ruby]. This is referred to as "monkeypatching" by Pythonistas and some others.