读书人

Ruby 元编程 步骤

发布时间: 2013-09-08 15:21:21 作者: rapoo

Ruby 元编程 方法

1.动态方法 (send && fine_method) -- 动态派发 , 动态创建, 动态内省 缩减代码

class MyOpneStruct  def initialize    @attributes = {}  end  def method_missing(name,*args)    attribute = name.to_s    if attribute =~ /=$/      @attributes[attribute.chop] = args[0]    else      @attributes[attribute]    end  endendicecream = MyOpneStruct.newicecream.flavor = "vanilla"icecream.flavor


读书人网 >编程

热点推荐