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

Metaprogramming

 Metaprogramming

Talk on basic metaprogramming based on presentations given by Dave Thomas at QCon '06 and Patrick Farley at MountainWest RubyConf '08.

Darth Vader art by Wight.

Michael Galero

May 22, 2008
Tweet

More Decks by Michael Galero

Other Decks in Programming

Transcript

  1. •Classes •Modules •Classes are Open •Singleton Class & Method •Method

    Receiver •Full Object Model •Metaprogramming Examples Ruby Concepts
  2. Classes Class Ninja < Human # class method def self.code_of_honor

    nil end # instance method def say(something) nil end end
  3. Modules module Taijutsu def punch(object) object.take_damage(2, :punched) end def kick(object)

    object.take_damage(3, :kicked) end end class Ninja include Taijutsu # mixin end
  4. Singleton Class & Method a = ‘foo’ def a.encrypt tr

    ‘a-z’, ‘b-za’ end b = ‘bar’ a.encrypt b.encrypt # error!
  5. Class method Ninja *super *klass m_tbl Object *klass m_tbl `Object

    *super m_tbl `Ninja *super m_tbl: code_of_honor Class *super m_tbl Module *super m_tbl
  6. include Module Ninja *super *klass m_tbl Object *klass m_tbl `Object

    *super m_tbl `Ninja *super m_tbl: code_of_honor (IClass) *super *klass *m_tbl Taijutsu m_tbl: code_of_honor