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

Ruby Meta-Programming for beginners

Ruby Meta-Programming for beginners

Presentation used for the OpenTechSchool Hackership (http://www.hackership.org/) introduction to Meta-Programming in Ruby mini-workshop.

Amir Friedman

December 03, 2013
Tweet

More Decks by Amir Friedman

Other Decks in Programming

Transcript

  1. whoami Amir Friedman • Developer at DaWanda (we’re hiring!) •

    Vegetarian • [email protected] • @NewArtRiot • http://github.com/psychocandy
  2. What are we going to learn • Clear uncertainty regarding

    MP • Show different layers of MP • Make you feel secure about MP • Show examples from the real-world
  3. The Ruby Ancestors Chain Let’s open pry (irb)! > self.class.ancestors

    => [Object, PP::ObjectMixin, Kernel, BasicObject] > class Greeting ; end > Greeting.ancestors => ? > module Hello ; end ...
  4. Ancestors Chain class Greeting if rand(2) == 0 include X

    else include Y end end We redirect messages during runtime :)
  5. 1st Layer • “Monkey Patching” (class, kernel method) • attr_…

    (attr_accessor, _reader, etc.) • alias :new_name :old_name