This is an article written by Ariel Ortiz,
our professor. Where he talks about metaprograms, which are programs that
generates other programs or program parts. So, metaprogramming means writing
metaprograms. The most common ones include compilers, interpreters, parser
generators, assemblers and preprocessors. We use metaprograms to reduce a
tedious programming task.
A quine is a special kind of source code
generator. It is a program that generates a copy of its own source as its
complete output.
Ruby is a dynamic language that allow us to
modify different parts of the program easily during runtime without having to
generate source code explicitly. Ruby´s core API and frameworks employ this
facility to automate common programming tasks. To accomplish the previous
statement we have code as the attr_initialize method that takes as input a
variable number of attribute name. Each of this attribute name has the same
position reserved for it in the dynamically created initialize methos parameter
“args” in order to set its initial value.
We use zip and each methods to iterate at
the same time over the attributes declared in list “attrs” and the argument
list “args”. As a final step we delegate the attr_accessor method to create the
read/write access methods for all the declared attributes.
Metaprogramming should sound difficult or
complicated, but once you are familiar with the techniques, it becomes easier.
With metaprogramming we can automate error-prone or repetitive programming
tasks. We can use it to pre-generate data tables, generate boilerplate code automatically
that you can abstract into a function, or even to test ingenuity on writing
self-replicating code.
No hay comentarios.:
Publicar un comentario