MacBookdeCasiano:rubytesting casiano$ cat -n searchmethod.rb 1 class A 2 def tutu 3 puts "A::tutu" 4 end 5 def method_missing(name, *args) 6 puts "In A::method missing" 7 end 8 end 9 10 class B < A 11 def method_missing(name, *args) 12 puts "In B::method missing" 13 end 14 end 15 16 x = B.new 17 x.tutu() # A::tutu 18 19 y = A.new 20 y.titi() # In A::method missing 21 22 z = B.new # In B::method missing 23 z.tete()
MacBookdeCasiano:rubytesting casiano$ ruby searchmethod.rb A::tutu In A::method missing In B::method missing
~/chapter8ReflectionandMetaprogramming$ cat -n method_missing.rb 1 class Shell 2 def method_missing(method, *args) 3 r = %x{#{method} #{args.join(' ')} 2>&1} 4 return r unless block_given? 5 yield r 6 end 7 end 8 9 s = Shell.new 10 puts s.ls('-l', 'r*.rb') { |x| 11 puts "inside block" 12 x.gsub(/^/,'** ') 13 } 14 puts s.uname
~/chapter8ReflectionandMetaprogramming$ ruby method_missing.rb inside block ** -rw-r--r-- 1 casianorodriguezleon staff 1195 1 nov 10:05 recipes.rb ** -rw-r--r-- 1 casianorodriguezleon staff 1194 1 nov 10:05 recipes2.rb ** -rw-r--r-- 1 casianorodriguezleon staff 483 12 nov 11:08 ruport_example.rb Darwin
~/chapter8ReflectionandMetaprogramming$ cat -n method_missing2.rb 1 def method_missing(method, *args) 2 r = %x{#{method} #{args.join(' ')} 2>&1} 3 return r unless block_given? 4 yield r 5 end 6 7 puts ls('-l', 'r*.rb') { |x| 8 puts "inside block" 9 x.gsub(/^/,'** ') 10 } 11 puts uname ~/chapter8ReflectionandMetaprogramming$ ruby method_missing2.rb inside block ** -rw-r--r-- 1 casianorodriguezleon staff 1195 1 nov 10:05 recipes.rb ** -rw-r--r-- 1 casianorodriguezleon staff 1194 1 nov 10:05 recipes2.rb ** -rw-r--r-- 1 casianorodriguezleon staff 483 12 nov 11:08 ruport_example.rb Darwin