Metaprogramming




With Ruby

Why?


      • Generating methods to 'reduce' duplicate code
      • Dynamic Dispatch
      • To temporarily patch a third party library

Why Not?


      • The target probably will change at some point
      • Metaprogramming code is often complex
      • Reduced readability
      • Harder to debug

So, what can I use?


      • module_eval
      • class_eval
      • instance_eval
      • eval
      • send
      • include
      • extend
      • prepend
      • define_method
      • method_missing

Instance_eval


class_eval


module_eval



Ha! I tricked you! 


module_eval and class_eval are



the same thing!

eval


Everyone, try this now:

BOOM! 

send


Actually, this can be really useful!


include


Extend


Prepend


define_method


method_missing



THANKS TO:


James Harton
 http://resistor.io

Pete Nicholls
http://metanation.com

Malcolm Locke
http://wholemeal.co.nz

Further reading:


  • "Metaprogramming in Ruby: It's All About the Self" http://bit.ly/9KfJlX