Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Metaprogramming isn't real, it can't hurt you

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Metaprogramming isn't real, it can't hurt you

Avatar for Masafumi Okura

Masafumi Okura

February 01, 2026
Tweet

More Decks by Masafumi Okura

Other Decks in Programming

Transcript

  1. Me

  2. me.introduce(self) • Name: Masa, OKURA Masafumi (Masafumi is the given

    name) • Job: Freelance web developer • Activities: Kaigi on Rails founder, Ruby community organizer, public speaker, OSS programmer (Alba is the most famous one) • Status: Job opportunities are welcome!
  3. 894

  4. Agenda 1. Introduction to metaprogramming 2. Metaprogramming usage and evolution

    in real world: Alba as an example 3. Lessons and thoughts 4. Conclusion: Metaprogramming isn’t real, it can’t hurt you
  5. “Metaprogramming is a computer programming technique in which computer programs

    have the ability to treat other programs as their data.” https://en.wikipedia.org/wiki/Metaprogramming
  6. Some examples of metaprogramming in Ruby • Module#de fi ne_method

    • A method that de fi nes a method • Kernel#eval • A method that treats strings as Ruby code • Object#send • A method that treats an argument as a method name
  7. More subtle examples • Hook methods such as Class#inherited and

    BasicObject#method_missing • It does something when Ruby does important things • Scope change such as BasicObject#instance_eval • It mimics as if the current self is that object • Class.new • A method that returns a new class that is de fi ned dynamically
  8. Some much more complex examples • Re fi nements •

    It modi fi es existing classes only in current fi le • TracePoint • It does something when Ruby does… things (even new line) • Binding • It returns a “binding”, which includes things such as current local variables
  9. Techniques used in Alba (referenced from the book) • Class

    Extension • Class Instance Variable • Class Macro • Deferred Evaluation • Dynamic Dispatch • Dynamic Method • Hook Method • String of Code
  10. When should we use metaprogramming? • When we need it

    and we cannot solve the problem otherwise • When we understand what we are doing well • When the components with metaprogramming is well encapsulated and isolated from other parts of the code
  11. When should we use metaprogramming? • When we need and

    it cannot solve the problem otherwise • KISS (Keep It Simple, Stupid) • When we understand what we are doing well • Of course :) • When the components with metaprogramming is well encapsulated and isolated from other parts of the code • Basic OOP principle
  12. When we should NOT use metaprogramming • When you don’t

    have enough knowledge about how Ruby works • Read “Metaprogramming Ruby” book fi rst! • When you work on something you don’t understand well enough • It tends to be over-engineering • When you work on something that must be stable • Stable interfaces must hide metaprogramming back
  13. Contact me! • Personal website: https://okuramasafumi.com • ruby.social: https://ruby.social/@okuramasafumi •

    X: https://x.com/okuramasafumi • GitHub: https://github.com/okuramasafumi • LinkedIn: https://www.linkedin.com/in/masafumi-okura-82651128/