CSC/ECE 517 Fall 2007/wiki1b 4 19: Difference between revisions
mNo edit summary |
|||
Line 5: | Line 5: | ||
This means that one can define classes and methods at runtime. It is possible only because of the dynamic nature of the language. | This means that one can define classes and methods at runtime. It is possible only because of the dynamic nature of the language. | ||
For | For instance one might take the example of taking input from a CSV[http://en.wikipedia.org/wiki/Comma-separated_values] file into a program. In such a case, if we are using static languages, we generally make a class or structure of the different fields in the file. We then make instances using the values. In another approach we simply use associative arrays, however this seems too unnatural and cumbersome. | ||
However in a dynamic language such as ruby, since we are now equipped with the powers of meta programming, we can create the class depending on the structure of the CSV file i.e. if the attribute names are stored in the first line of the file, we can create a class with these as variables, at runtime, on the fly and then instantiate. | |||
For possible meta programming implementations of the CSV problem in Ruby, I would refer you to [http://www.devsource.com/article2/0,1895,1928561,00.asp] | |||
==Under the hood== | ==Under the hood== |
Revision as of 00:03, 11 October 2007
Metaprogramming in Ruby
Introduction to metaprogramming
Metaprogramming,just as the name suggests, means “Programming a program”. This means that one can define classes and methods at runtime. It is possible only because of the dynamic nature of the language.
For instance one might take the example of taking input from a CSV[1] file into a program. In such a case, if we are using static languages, we generally make a class or structure of the different fields in the file. We then make instances using the values. In another approach we simply use associative arrays, however this seems too unnatural and cumbersome.
However in a dynamic language such as ruby, since we are now equipped with the powers of meta programming, we can create the class depending on the structure of the CSV file i.e. if the attribute names are stored in the first line of the file, we can create a class with these as variables, at runtime, on the fly and then instantiate.
For possible meta programming implementations of the CSV problem in Ruby, I would refer you to [2]
Under the hood
So you think how does ruby achieve this great feat. Well its not rocket science. Every object in ruby has a associated class that is accessible to only that object. Now recall that everything in ruby is an object. Put two and two together and you realize that classes and modules are also objects and so they too have classes, all for themselves.
These classes are called Singleton classes(accessible to only a single object). So when you send a message to an object, its singleton class is first checked to see if it has methods defined that can handle this message.
Metaprogramming is made possible because of singleton classes. Whenever we dynamically define a method on an object, it is added as a method of the singleton class for this object.
For an excellent discussion about singleton classes and their properties, [3] is a must read.
Guide
So one might think, well I can write code that will generate code, what good can that do. Well then you must read further.
There is an excellent blog entry on the classification of metaprogramming into different types (based on usability) I dont think I could do a better job of explaining metaprogramming in ruby concisely. Thus, I refer you to [4]
To help you along the way, here is some information
- DSL stands for domain specific language(for point 2)
- Point 4 gives some more uses of method missing besides the roman numeral example, we went through.
- Point 6 is similar to the wrapping scenario discussed in class.
Some other useful links
A presentation made at a conference [5]
About Metaclasses [6]