1. ruby元编程的含义:
Ruby中的元编程,是可以在运行时动态的操作语言结构(如类、模块、实例变量等)的技术。你甚至可以在不用重启的的情况下,在运行时直接键入一段新的Ruby代码,并执行他。 Ruby的元编程,也具有“利用代码来编写代码”的作用,例如,常见的attr_accesstor等方法就是如此。
-- 摘自Ruby元编程
2. Ruby元编程常见的使用:
Ruby元编程技术(Ruby Metaprogramming techniques)
3. Ruby基础知识:
4. Ruby元编程5种匿名类的方法:
1 # 1 2 class Rubyist 3 def self.who 4 "Geek" 5 end 6 end 7 8 # 2 9 class Rubyist 10 class << self 11 def who 12 "Geek" 13 end 14 end 15 end 16 17 # 3 18 class Rubyist 19 end 20 def Rubyist.who 21 "Geek" 22 end 23 24 #4 25 class Rubyist 26 end 27 Rubyist.instance_eval do 28 def who 29 "Geek" 30 end 31 end 32 puts Rubyist.who # => Geek 33 34 # 5 35 class << Rubyist 36 def who 37 "Geek" 38 end 39 end