Skip to main content

Posts

Showing posts from December, 2018

Starting with Ruby Metaprogamming

Metaprogramming  is a programming technique in which computer programs have the ability to treat programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself while running.(wikipedia)  Using simple code samples let's give basic start to metaprogramming which helps to relate how code base can be simplified.  Considering the below class. Now, using some metaprogramming concepts above code can be modified, notice the assign_attributes method used while the o/p in both the examples is same Adding some more examples, below simple code helps to generate methods on the fly instead of defining in individually Metaprogramming should be used with caution and not make the code complicated and difficult to read. It should be easy to understand and modular to change in future.