想用 method_missing构造动态方法,以"can_" 开头
ruby 代码
- class User < ActiveRecord::Base
- def has_role?(role_in_question)
- @_list ||= self.roles.collect(&:name)
- return true if @_list.include?("admin")
- (@_list.include?(role_in_question.to_s) )
- end
-
- def method_missing(method_id,arg)
- method_name = method_id.id2name
- arr = method_name.split("_")
- if arr[0] == "can"
- has_role?(arg)
- else
- raise NoMethodError,"there is no method:#{method_name}"
- end
- end
- end
我跟踪后发现不执行此方法
分享到:
- 2007-12-27 09:16
- 浏览 895
- 评论(1)
- 论坛回复 / 浏览 (1 / 1879)
- 查看更多
相关推荐
主要介绍了Ruby元编程之梦中情人method_missing方法详解,本文讲解了我该怎么用 method_missing 、方法代理、define_method、什么时候用 method_missing、元方法等内容,需要的朋友可以参考下
主要介绍了ruby元编程之method_missing的一个使用细节,本文介绍在使用method_missing时造成死循环的一个现象,需要的朋友可以参考下
gem install method_not_missing 外部依赖 PhantomJS(在OSX上brew install phantomjs ) 例子 require "method_not_missing" object = MethodNotMissing :: OmnipotentObject . new object . update ( [ 3 ] ) ## ...
本代码是个人编写的计算GaN量子阱内导带,价带(6*6哈密顿量)的光学特性,包含光吸收,光增益,量子效率,波函数,计算包含导带-价带,导带-导带,价带-价带内部的跃迁的所有计算,使计算量子体系的光吸收的理想...
当有try-catch-finally语句的时候会提示信息“Couldn't resolve all exception handlers in method ” 4. Currently Jad ignores the contents of the Line Number Table Attribute and the Source File Attribute...
__id__ initialize method_missing singleton_method_undefined != __send__ instance_eval singleton_method_added == equal? instance_exec singleton_method_removed Kernel !~ enum_for
method_missing是Ruby元编程(metaprogramming)常用的手法。基本思想是通过实现调用不存在的方法,以便进行回调。典型的例子是:ActiveRecord的动态查找(dynamic finder)。例如:我们有email属性那么就可以调用...
第 10 周:创客学院在 Ruby 中玩转元编程挑战: 使用method_missing重新定义对象在询问has_unknown_attribute时的Reacthas_unknown_attribute 。 使用define_method创建Ruby 内置attr_accessor方法的布尔版本。代码...
使用send , define_method和method_missing遍历不同的元编程技术 将您的思维扩展到元编程,并让您考虑可以在自己的代码中利用它的地方 设置 要求 该讲习班是使用Ruby 1.9.3创建的,但是在2.1.2版本中有效。 假设您...
我们建立在 HTTParty 之上,并做了一些method_missing魔法来接受官方 Flickr API 支持的任何方法。 您还可以(可选)使用auth_url和complete_auth方法(见下文)进行经过身份验证的 API 调用。 安装 gem install ...
attributes, :method_missing, :assign_attribute, :read_attribute, :read_new_model, :new_model , :new_array_model, :expand!, :<<, :save!, :promise_for_errors, :buffer, :state,
它覆盖[] 、 find_all和method_missing以提供一种简化的 DSL,用于通过查询对象的属性来查找对象。 在查看完整文档。 安装 gem install queryable_array 要求 Ruby 1.9+ 用法 基本¶ ↑ 使用一组对象初始化...
没有method_missing调用 带:collection选项的render_partial实际上会渲染集合(与Jbuilder不同) 句法 .jb模板应包含Ruby代码,该代码将返回任何to_json (通常为Hash或Array)的Ruby对象。 然后,返回值将to_json...
蛋白石路轨 Rails绑定。 ()寻找Webpack支持? :eyes: 如果要通过Webpack... method_missing = trueRails . application . config . opal . optimized_operators = trueRails . application . config . opal . arity_
相反,它只不过是几行代码,一个方法编译,没有method_missing和魔术。 另外,调用钩子时,可以将其他参数传递给回调函数。 例子 让我们...一只猫。 require 'hooks' class Cat include Hooks define_hooks :...
了解有关method_missing用于动态范围的更多信息 了解有关更多信息 视频元数据访问API 用于访问视频元数据信息的API。 您可以在找到数据库的架构设计 视频方法 输出格式为JSON。 POST api /视频 添加新的视频条目。 ...
与rails-settings和forks相反, method_missing不在任何地方使用。 欢呼! 支持的版本 最新版本 Rails 4.1和4.2 Ruby 2.2 SQLite,MySQL和PostgreSQL 每个有效的组合都。 用法 假设您的配置是通过“ S