meta_programming中的method_missing
来源:互联网 发布:数据库建模工具哪个好 编辑:程序博客网 时间:2024/06/18 14:21
当你使用ruby的activerecord的时候,你是否会经常遇到以下的程序了
Post.find_by_title("Awesomeness!") User.find_by_email("bob@example.com") User.find_by_email_and_login("bob@example.com", "bob")
那么这些程序是如何实现的呢?
让我们来看一下源代码吧!
class ActiveRecord::Base def method_missing(meth, *args, &block) if meth.to_s =~ /^find_by_(.+)$/ run_find_by_method($1, *args, &block) else super # You *must* call super if you don't handle the # method, otherwise you'll mess up Ruby's method # lookup. end end def run_find_by_method(attrs, *args, &block) # Make an array of attribute names attrs = attrs.split('_and_') # #transpose will zip the two arrays together like so: # [[:a, :b, :c], [1, 2, 3]].transpose # # => [[:a, 1], [:b, 2], [:c, 3]] attrs_with_args = [attrs, args].transpose # Hash[] will take the passed associative array and turn it # into a hash like so: # Hash[[[:a, 2], [:b, 4]]] # => { :a => 2, :b => 4 } conditions = Hash[attrs_with_args] # #where and #all are new AREL goodness that will find all # records matching our conditions where(conditions).all end end
当你执行find_by_mail 的时候,你发现class中没有这个函数定义, 这个时候系统就会转向method_missing 函数
进入函数就遇到一个正规表达式, 如果函数名开头是以find_by_开头的, 就当作匹配成功,这个时候就要执行run_find_by_method
而这个函数的参数里面出现了$1, 这个是用来获取
/^find_by_(.+)$/ 括号里面的参数。
比如find_by_mail $1 = 'mail'
find_by_mail_and_login $1 = 'mail_and_login'
函数进去之后,就先分割字符串
比如说 ‘mail_and_login’ 分割为 ['mail', 'login']
下一步就是对号入座
[['mail', 'login'], ['bob@163.com', 'bob']]
再将数组变换为hash
{ :mail => 'bob@163.com', :login => 'bob'}
是不是突然有一种ruby很高深的感觉了。。。。。。。。。。。。。。。。。。
- meta_programming中的method_missing
- method_missing,一个 Ruby 程序员的梦中情人
- Ruby China中method_missing和define_method剖析
- ruby元编程之 method_missing 一个细节
- [Ruby笔记]21.Ruby public_method_defined? 以及 method_missing
- Ruby China中method_missing和define_method剖析
- 使用method_missing和respond_to?创建自己的动态方法
- 使用method_missing和respond_to?创建自己的动态方法
- Ruby的method_missing可以定义client调用不存在的方法时的行为
- 中的句子
- 偿中的
- 单片机中的“$”
- Java 中的 & | ^
- 中的Container
- C#中的@
- classpath中的".;"
- MASM中的@@
- C中的#
- ADOPENKEYSET ADOPENDYNAMIC
- 7z使用中,关于解压时的覆盖提示和子文件夹解压失败处理方法
- webrtc的scoped_ptr
- android_时间格式
- UIControl 使用全面解析
- meta_programming中的method_missing
- C/C++程序中致命的内存分配问题
- 外国一个比较全的操作系统开发网站
- WebService学习笔记(二)
- Mysql:is not allowed to connect to this MySQL server
- 16进制转换成10进制
- 设置crichedit的文字颜色和背景颜色
- 一个通过Java连接MySQL数据库的代码
- erdas中怎样给无坐标系统的数据定义坐标系统