Ruby Metaprogramming 08


Published on

An introductory presentation about Ruby metaprogramming presented at CodeMash '08

Published in: Technology

Ruby Metaprogramming 08

  1. Rails A Peek under the covers Brian Sam-Bodden
  2. Rails
  3. A framework... for building database-backed web applications that at first wow us with speed
  4. Rails can be seen as a collection of... mini-languages for implementing MVC on the web
  5. then came the headlines... and we all wondered... what am I doing wrong?
  6. the answer too much and we reacted the way we knew better ...with frameworks
  7. Lessons of Rails
  8. Integrated Seamless front to back stack Familiar idioms on every tier Avoiding manually defining or gluing the tiers
  9. CoC Convention Over Configuration “Flexibility is overrated, Constraints are liberating”
  10. DRY Do it once, do it right and do it in the right place
  11. Instant Change Tight feedback loop We want the world and we want it NOW!
  12. Terse Expressiveness Getting advanced behavior with very little code Without sacrificing readability
  13. Did the clones catch up? Yes , Sort of and Not quite ✓CoC ✓DRY ๏ Terse Expressiveness ✓ Instant Change ✓Integrated
  14. What’s Missing?
  15. ✓Object-Oriented ✓Elegant ✓General Purpose ✓ Multi-paradigm Ruby ✓ Interpreted is... ✓Reflective ✓Dynamic ✓ Garbage-collected
  16. Without Ruby there would be no Rails Ruby’s Dynamism is the key ingredient to the Rails framework
  17. Open Classes
  18. Say we have an Array like: [1,2,3,4,5] and we want to sum of all of its elements
  19. Let’s try something like: [1,2,3,4,5].sum Doh! Doesn’t work in Ruby … yet!
  20. Ruby classes are open We can teach the Array class a new behavior
  21. Let’s try again! Load the file containing the enhancements
  22. Since Ruby classes are open... You can statically enhance ANY class
  23. Code as Data
  24. When code can be manipulated as data a whole world of possibilities opens In Ruby you can evaluate the contents of a string
  25. Code that spits out code
  26. Ruby provides the eval family of methods for runtime execution of code stored in strings eval will evaluate any string
  27. instance_eval can evaluate a string or a code block in the context of the receiver
  28. class_eval can evaluate a string or a code block in the context of the class or module it is called on
  29. Meta-programming
  30. Meta-programming about programs that write programs’s a superb tool for building frameworks’s the key ingredient for building domain-specific languages
  31. Ruby is a great language for meta-programming because dynamic and reflexive and malleable ...code is data, data is code ...clean unencumbered syntax ...programming event model
  32. Rails ...uses the Ruby meta-programming techniques to accomplish most of its “magic” ...uses meta-programming to bring the language closer to the problem at hand a domain-specific language for building web applications
  33. Singleton Class
  34. All objects are open to modification You can change a particular instance of a class Ruby uses a proxy class known as the singleton class Meta-class: The singleton for Class objects
  35. With access to a class object’s meta-class we can then use Ruby meta-programming techniques to enhance the class
  36. Rails relies heavily on Ruby’s ability to dynamically enhance a class
  37. The previous example could have also been accomplished using the define_method
  38. Accessing the singleton meta-class explicitly
  39. Inheritance the Ruby Way
  40. Ruby Meta-programming techniques can be used to write DSLs that use class methods to enhance subclasses Using the meta-class (singleton class) of the subclasses Rails uses this technique in ActiveRecord
  41. Let’s create a module with a “Base” class
  42. Methods get added to our classes with simple declarations Can you say DSL!
  43. Rails uses this technique in ActiveRecord Now our class has a new set of class methods
  44. AOP the Ruby Way
  45. With alias_method you can wrap an existing method
  46. Meta-Programming Events
  47. Included Hook
  48. Ruby has a rich event model associated with static and dynamic changes of the code
  49. Method Missing
  50. Ruby provides several hook methods that can be used to created custom behaviors method_missing method_added method_removed
  51. Let’s implement the greeter example using method_missing
  52. There is more to Ruby than Rails! Building a Framework? Give Ruby a try! Build the language up towards the problem Meta-programming is no longer just for the Lisp folks The future? Powerful Java APIs exposed with Ruby DSLs