Successfully reported this slideshow.

Beauty of metaprogramming

1,097 views

Published on

Basic introduction to ruby metaprogramming. Big thanks to Dave Thomas for his tutorials on metaprogramming

  • Be the first to comment

Beauty of metaprogramming

  1. 1. Beauty ofMetaprogrammingin RubyS. M. Ashif Manjur@Nascenia
  2. 2. • What is Metaprogramming?• Ruby Object Model• Sharing functionalities• instance_eval and class_eval• Magic of method_missingTopics I will touch…
  3. 3. “Writing code that writes code”WHAT IS METAPROGRAMMING?
  4. 4. METHOD LOOKUP CHAIN
  5. 5. • Classes are always open• Class definitions are live• Classes are objects• Every method call has a receiver• Anonymous or ghost classesCLASSES
  6. 6. CONSOLE OUTPUT: $$$ CAROPEN CLASSES
  7. 7. “with great power comes great responsibility”AS UNCLE BEN SAYS…
  8. 8. CONSOLE OUTPUT: $$$ CARMONKEY PATCHING
  9. 9. DEFINITIONS ARE LIVE
  10. 10. CLASSES ARE OBJECTS
  11. 11. SELF AS A RECEIVER
  12. 12. SINGLETON METHODS
  13. 13. GHOST CLASSES
  14. 14. CLASS METHODS ARE SAME
  15. 15. • Prototype• Inheritance• MixinSHARING FUNCTIONALITY
  16. 16. PROTOTYPE
  17. 17. INHERITANCE
  18. 18. MIXIN
  19. 19. AFTER MIX(ED) IN…
  20. 20. EVALS ARE EVIL
  21. 21. instance_eval
  22. 22. class_eval
  23. 23. METHOD MISSING
  24. 24. Q&A
  25. 25. THANK YOU

×