`
gaofan
  • 浏览: 895 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

method_missing 的问题

阅读更多
想用 method_missing构造动态方法,以"can_" 开头
ruby 代码
  1. class User < ActiveRecord::Base   
  2.     def has_role?(role_in_question)   
  3.     @_list ||= self.roles.collect(&:name)   
  4.     return true if @_list.include?("admin")   
  5.     (@_list.include?(role_in_question.to_s) )   
  6.   end  
  7.      
  8.     def method_missing(method_id,arg)   
  9.     method_name = method_id.id2name   
  10.     arr = method_name.split("_")   
  11.     if arr[0] == "can"  
  12.               has_role?(arg)   
  13.     else  
  14.         raise NoMethodError,"there is no method:#{method_name}"  
  15.     end  
  16.   end  
  17. end  
我跟踪后发现不执行此方法
分享到:
评论
1 楼 刑天战士 2007-12-28  
*args吧……

相关推荐

Global site tag (gtag.js) - Google Analytics