Metaprogramming at its heart is the creation of programs that can be dynamically altered at runtime as well as write whole new programs. This method of programming can potentially save you time and let you create some interesting and useful programs. For example with metaprogramming you can generate boilerplate code, create advanced customizable applications and create efficient code to name a few things.
A great example of applications that utilise metaprogramming are IDE’s such as Microsoft Visual Studio. Every time that a new project is created with an IDE the application has to generate hundreds or even thousands of lines of boilerplate code to ensure that the user can begin working on their program with ease. This dynamically created code allows projects within IDE’s to have a wide range of customisable settings and increases ease of use.
Ruby is one of the best languages available for metaprogramming. Ruby is special because it allows classes and methods to be redefined at runtime, which is perfect for metaprogramming and is an unusual feature in object oriented programming languages.
Creating A Quine
A quine is a program that prints out a copy of its own source code. Legitimate quine’s cannot receive any input, including file reading, this means a quine is not allowed to read its own source code file. Quine’s are one of the most basic types of metaprograms so creating one is a great way to begin to understand the structure of metaprograms and the principles that guide metaprogramming.
A simple way to begin writing a quine is to create something along the lines of the following:
quine = “puts quine” puts quine
This is almost a quine but falls short because the defining of the quine variable isn’t printed to the screen. You can’t just put the definition of quine within the string because that wouldn’t actually cause the whole program to be printed but instead just start an infinite loop of adding “quine =” to the string.
The solution to this is to use the .inspect method in your quine. Using the inspect method returns the value of a string inserted within double quote marks. This will allow the program to print itself accurately without any issue.
quine = "\nputs \"quine = \" + quine.inspect + quine" puts "quine = " + quine.inspect + quine
This program will now completely print out its own source code in full without breaking any of the quine rules. The .inspect method allows the program to print the full definition of the variable without the infinite loop issue that would have occurred earlier and then you can concatenate quine onto the end of the string that will allow both lines of code in the program to print to the screen.
It may not seem like much but the quine that you created above is a fully functioning metaprogram. Every time you run the program its output is a fully functioning program which could be copied into an .rb file and it would run without issue, creating yet another program which could be copied and so on forever.
Of course it also just so happens that the program it’s outputting is an exact copy of itself but that’s only the beginning. A quine is purely academic in nature but it’s the first step towards creating programs that write completely new code with different functionality.
Next up in part two we will continue exploring concepts and techniques within metaprogramming. We’ll also begin creating more advanced metaprograms that can create unique code as opposed to just copying themselves.
Download the above source code here.
If you have any comments or questions email them to me at firstname.lastname@example.org.