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

This Talk is so Meta

This Talk is so Meta

Introduction to metaprogramming in ruby

Nugroho Herucahyono

September 26, 2013

Transcript

64 slides
  1. Metaprogramming: the writing of computer programs that write or manipulate

    other programs (or themselves) as their data, or that do potato potato potato... Metaprogramming
  2. - Meet your first Meta - attr_accessor - The basic

    - block - ruby class structure - method lookup - eigenclass - The technique - define_method - method_missing - method cache - class_eval - instance_eval - eval Agenda
  3. class Person { private String name; public String getName() {

    return this.name; } public void setName(String name) { this.name = name; } } attr_accessor
  4. A block is a just chunk of code that can

    be called 3.times do puts “hello” end block
  5. A block is just a special form of a proc

    def show_me_the_block(&block) block end block vs proc
  6. $ b = show_me_the_block do puts “hello” end => #<Proc:0x00000000d8d600:4>

    $ b.call => Hello $ b.class => Proc block vs proc
  7. my_proc = proc { puts “hello” } my_proc.call => hello

    3.times &my_proc => hello hello hello block vs proc
  8. A block is a closure because it can access its

    environment name = “xinuc” 3.times do puts “hello #{name}” end block & closure
  9. o = Object.new o.hello => NoMethodError: undefined method `hello' def

    o.hello puts “hello” end o.hello => hello eigen_class
  10. $ shout => undefined local variable or method `shout' $

    learn_to_shout => learning.... $ shout => Wazzzaaaap!! Define method
  11. def who_are_you puts “I'm batman” def who_are_you puts “I'm superman”

    def who_are_you puts “I'm ironman” end end end Define method
  12. def who_are_you puts “I'm batman” def who_are_you puts “I'm superman”

    def who_are_you puts “I'm ironman” end end end Hypocrite method
  13. def who_are_you puts “I'm batman” def who_are_you puts “I'm superman”

    def who_are_you puts “I'm ironman” end end end Yo dawg method
  14. define_method method takes a method name and a block, then

    creates a method with block as the method body define_method :name do |args| # method body end define_method method
  15. def attr_accessor(*attrs) attrs.each do |attr| define_method attr do instance_variable_get “@#{attr}”

    end define_method “#{attr}=” do |val| instance_variable_set “@#{attr}”, val end end end attr_accessor
  16. class Person def method_missing(name, *args, &block) puts “method #{name}” end

    end p = Person.new p.omg_lol => method omg_lol method_missing
  17. class Person def method_missing(name, *args, &block) self.class.class_eval do define_method(name) do

    puts ”method #{defined} defined” end end self.send name, *args end end Lazy method definition
  18. p = Person.new p.respond_to? :hello => false p.hello => method

    hello defined p.respond_to? :hello => true Lazy method definition
  19. class Person end p = Person.new p.instance_eval do def hello

    puts “hello” end end instance_eval
  20. class Person end p = Person.new p.singleton_class.class_eval do def hello

    puts “hello” end end p.hello instance_eval
  21. instance_eval change the “self” inside a block s = “Hello”

    s.instance_eval do puts self.upcase end instance_eval
  22. # sunspot class User < ActiveRecord::Base # method text &

    integer # not defined here searchable do # but we can call method text & integer here text :name integer :age end end instance_eval

More Decks by Nugroho Herucahyono

Other Decks in Programming