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

Content added Content deleted
(Excluded Scala)
No edit summary
Line 1: Line 1:
{{task|Object oriented}}
{{task|Object oriented}}
{{omit from|Scala}}
This demonstrates how to dynamically add variables to a class instance at runtime. This is useful when the methods/variables 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. It's possible in most '''dynamic''' OO languages such as Python, Ruby, and Smalltalk.
This demonstrates how to dynamically add variables to a class instance at runtime. This is useful when the methods/variables 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. It's possible in most '''dynamic''' OO languages such as Python, Ruby, and Smalltalk.


Line 548: Line 547:
{{omit from|M4}}
{{omit from|M4}}
{{omit from|Make}}
{{omit from|Make}}
{{omit from|OCaml}}
{{omit from|Octave}}
{{omit from|Octave}}
{{omit from|Pascal}}
{{omit from|Pascal}}
{{omit from|PlainTeX}}
{{omit from|PlainTeX}}
{{omit from|R}}
{{omit from|R}}
{{omit from|Scala}}
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have objects. -->
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have objects. -->