- 浏览: 58306 次
- 性别:
- 来自: 深圳
最新评论
对 于alias, alias_method, alias_method_chain的深入理解是有益的,因为rails3的源码里很多地方使用了alias_method_chain的魔法。 有人评论说alias_method_chain使用的过多不好,具体怎么不好,是后话了,这篇文章集中在理解这3个方法上面。
如果想转载本文,请注明出处,谢谢!请尊重别人的劳动成果,为构建丰富web原创内容做贡献!
1. alias
Ruby里的关键字,用于定义方法或者全局变量的别名。 例子:
class A
def m1
puts "m1"
end
alias m2 m1
end
=> nil
a = A.new
=> #<A:0xb7ef5234>
a.m1
m1
=> nil
a.m2
m1
=> nil
在使用的时候,注意原有的方法名在最后位置,用空格分开。
2. alias_method
作用和alias差不多,是Module的一个私有实例方法,只能用于给方法起别名,并且参数只能是字符串或者符号(alias后面跟的直接是方法名,不是字符串也不是符号)。例子:
class B
def b
p "b"
end
alias_method :c, :b
end
=> B
b = B.new
=> #<B:0xb7ee75bc>
b.c
"b"
=> nil
b.b
"b"
=> nil
注意,alias_method的参数必须是字符串或者是符号,并且用逗号分隔。
这样也可以:
module A
def b
p "b"
end
alias_method 'c', 'b'
end
class B
include A
end
#=> B
b = B.new
#=> #<B:0xb7ee75bc>
b.c
#"b"
#=> nil
b.b
#"b"
#=> nil
3. alias_method_chain
是ActiveSupport的一个公有实例方法。同样接受两个参数,可以是符号,也可以是字符串,但要注意一下第1个参数才是原始方法(alias_method的第2个参数是原始方法)。例子:
class A
def m1
puts 'm1'
end
def m1_with_m2
puts "do something befor m1"
m1_without_m2
puts "do something after m2"
end
alias_method_chain :m1, :m2
end
=> A
a = A.new
=> #<A:0xb7bd9820>
a.m1
do something befor m1
m1
do something after m2
=> nil
上面的代码用alias或者alias_method也能完成:
class A
def m1
puts 'm1'
end
alias m1_without_m2 m1
def m1_with_m2
puts 'do something else'
m1_without_m2
end
alias m1 m1_with_m2
end
那么其原理也一目了然了:
a = A.new
a.m1
当调用m1的时候, m1_with_m2会执行, 在puts "do something befor m1"之后,执行m1_without_m2,这个时候是执行了真正的m1方法。 这样就形成了一个类似于AOP的行为。
也可以说,对外把m1方法隐藏起来了,对类外部,实际上把m1_with_m2改头换面已经成为了另一个方法,只是我们不知道而已,因为它还叫m1.
再来看看alias_method_chain的源码:
def alias_method_chain(target, feature)
# Strip out punctuation on predicates or bang methods since
# e.g. target?_without_feature is not a valid method name.
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
yield(aliased_target, punctuation) if block_given?
with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
alias_method without_method, target
alias_method target, with_method
case
when public_method_defined?(without_method)
public target
when protected_method_defined?(without_method)
protected target
when private_method_defined?(without_method)
private target
end
end
一个道理。
更实际的例子:
在一些rails比较老的系统里,搜索功能的日期选择可能会用到date_select,这个方法会生成类似于这样的页面元素:
search_form[start_from(1i)]年
search_form[start_from(2i)]月
search_form[start_from(3i)]日
把这样的参数传回去,就无法查询到对应的日期。这个时候我们需要在后台得到查询条件之后来处理日期,比如:
get_conditions 这个方法假如是得到页面查询条件的,它返回一个数组,这个时候我们可以定义:
def get_conditions_with_handle_date
puts "你可以在get_conditions方法执行前干点别的,如果你愿意"
get_conditions_without_handle_date
puts "get_conditions执行完了,我们可以在其后干点别的,比如说处理日期"
conditions.reject!{|condition|condition[0] =~ /\([1-3]i\)/} # 把条件数组里的1i,2i,3i之类的去掉。
conditions << ["? <= #{@model.table_name}.created_at", @search.start_from] if @search.start_from #给搜索对象里添加正确的查询日期条件
conditions << ["#{@model.table_name}.created_at < ?", @search.end_to + 1.day] if @search.end_to #给搜索对象里添加正确的查询日期条件
end
#然后实施魔法
alias_method_chain :get_conditions, :handle_date
这样我们就搞定了。
本文出自 “{ :Alex Space => &..” 博客,请务必保留此出处http://blackanger.blog.51cto.com/140924/355102
如果想转载本文,请注明出处,谢谢!请尊重别人的劳动成果,为构建丰富web原创内容做贡献!
1. alias
Ruby里的关键字,用于定义方法或者全局变量的别名。 例子:
class A
def m1
puts "m1"
end
alias m2 m1
end
=> nil
a = A.new
=> #<A:0xb7ef5234>
a.m1
m1
=> nil
a.m2
m1
=> nil
在使用的时候,注意原有的方法名在最后位置,用空格分开。
2. alias_method
作用和alias差不多,是Module的一个私有实例方法,只能用于给方法起别名,并且参数只能是字符串或者符号(alias后面跟的直接是方法名,不是字符串也不是符号)。例子:
class B
def b
p "b"
end
alias_method :c, :b
end
=> B
b = B.new
=> #<B:0xb7ee75bc>
b.c
"b"
=> nil
b.b
"b"
=> nil
注意,alias_method的参数必须是字符串或者是符号,并且用逗号分隔。
这样也可以:
module A
def b
p "b"
end
alias_method 'c', 'b'
end
class B
include A
end
#=> B
b = B.new
#=> #<B:0xb7ee75bc>
b.c
#"b"
#=> nil
b.b
#"b"
#=> nil
3. alias_method_chain
是ActiveSupport的一个公有实例方法。同样接受两个参数,可以是符号,也可以是字符串,但要注意一下第1个参数才是原始方法(alias_method的第2个参数是原始方法)。例子:
class A
def m1
puts 'm1'
end
def m1_with_m2
puts "do something befor m1"
m1_without_m2
puts "do something after m2"
end
alias_method_chain :m1, :m2
end
=> A
a = A.new
=> #<A:0xb7bd9820>
a.m1
do something befor m1
m1
do something after m2
=> nil
上面的代码用alias或者alias_method也能完成:
class A
def m1
puts 'm1'
end
alias m1_without_m2 m1
def m1_with_m2
puts 'do something else'
m1_without_m2
end
alias m1 m1_with_m2
end
那么其原理也一目了然了:
a = A.new
a.m1
当调用m1的时候, m1_with_m2会执行, 在puts "do something befor m1"之后,执行m1_without_m2,这个时候是执行了真正的m1方法。 这样就形成了一个类似于AOP的行为。
也可以说,对外把m1方法隐藏起来了,对类外部,实际上把m1_with_m2改头换面已经成为了另一个方法,只是我们不知道而已,因为它还叫m1.
再来看看alias_method_chain的源码:
def alias_method_chain(target, feature)
# Strip out punctuation on predicates or bang methods since
# e.g. target?_without_feature is not a valid method name.
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
yield(aliased_target, punctuation) if block_given?
with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
alias_method without_method, target
alias_method target, with_method
case
when public_method_defined?(without_method)
public target
when protected_method_defined?(without_method)
protected target
when private_method_defined?(without_method)
private target
end
end
一个道理。
更实际的例子:
在一些rails比较老的系统里,搜索功能的日期选择可能会用到date_select,这个方法会生成类似于这样的页面元素:
search_form[start_from(1i)]年
search_form[start_from(2i)]月
search_form[start_from(3i)]日
把这样的参数传回去,就无法查询到对应的日期。这个时候我们需要在后台得到查询条件之后来处理日期,比如:
get_conditions 这个方法假如是得到页面查询条件的,它返回一个数组,这个时候我们可以定义:
def get_conditions_with_handle_date
puts "你可以在get_conditions方法执行前干点别的,如果你愿意"
get_conditions_without_handle_date
puts "get_conditions执行完了,我们可以在其后干点别的,比如说处理日期"
conditions.reject!{|condition|condition[0] =~ /\([1-3]i\)/} # 把条件数组里的1i,2i,3i之类的去掉。
conditions << ["? <= #{@model.table_name}.created_at", @search.start_from] if @search.start_from #给搜索对象里添加正确的查询日期条件
conditions << ["#{@model.table_name}.created_at < ?", @search.end_to + 1.day] if @search.end_to #给搜索对象里添加正确的查询日期条件
end
#然后实施魔法
alias_method_chain :get_conditions, :handle_date
这样我们就搞定了。
本文出自 “{ :Alex Space => &..” 博客,请务必保留此出处http://blackanger.blog.51cto.com/140924/355102
发表评论
-
Diaspora 功能特性列表
2013-05-10 14:25 975Diaspora Function Features List ... -
rails scaffold getting the column list from existing table
2011-09-23 10:02 879之前一直找这个问题的解决办法,但一直没找到,在g上看到了一个人 ... -
ruby语法-传值
2011-08-23 16:49 1018=begin def test1(*a) p a.firs ... -
Rails 3 用 JQuery 替代 Prototype
2011-07-28 22:48 10951.进入到工程目录中修改Gemfile文件,加入如下行: g ... -
Generate a list of Rails controllers and methods
2011-07-01 17:40 819列出项目中的controler和他对应的actions: @c ... -
Ubuntu手动安装JDK
2011-05-20 16:03 537Ubuntu手动安装JDK 安装前可以查看一下jdk版本,进 ... -
bundler
2011-04-25 12:12 802Rails3里多了个Bundler工具。 它是一个Librar ... -
研究小计1
2011-04-20 00:55 631有时候对一些基础的东西,不知道它的边界在哪里,其实做些小实验就 ... -
RubyGem version error: rack(1.0.0 not ~> 1.0.1)
2011-04-17 00:09 1326I have this error when I try to ... -
rubyonrails Restful Authentication 插件
2011-04-17 00:01 779restful_authentication是Rails的一个 ... -
XP.CMD命令大全
2011-04-17 00:00 608有关某个命令的详细信 ... -
几个有用的方法
2011-04-16 23:58 557hash.invert Returns a new hash ... -
Firefox键盘快捷方式
2011-04-16 23:55 1184Firefox键盘快捷方式 键盘快捷健 下面是 Mozil ... -
Ubuntu手动安装JDK
2011-04-16 23:53 706Ubuntu手动安装JDK 安装前可以查看一下jdk版本,进 ... -
字符转时间格式转换成时间对象
2011-04-16 23:52 716Time.parse("Wed, 23 Jan 20 ... -
使用Rails时遇到了服务器启动问题 script/server:3
2011-04-16 23:47 1259境 Windows Vista Home Premium c ... -
DoDirectPayment
2011-04-16 23:44 970DoDirectPayment 1. DoDirectPay ... -
Rails异常处理
2011-04-16 23:43 939异常处理是开发过程中 ... -
ruby和rails中的回调函数
2011-04-16 23:41 1850Ruby中Class,Object,Module这三个类定义了 ... -
Rails3:使用bundler管理gems
2011-04-16 23:39 1367bundler是一套为了 Rails3 所打造的全新 Gem ...
相关推荐
int connect(int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS_C(connect); // 发送数据 ssize_t send(int, const void *, size_t, int) __DARWIN_ALIAS_C(send); // void * 万能指针 // 读取数据 ssize_t...
nangua_envTestWithDevKit_V4.1.7_2108061935_alias_b.apk
case_alias(病例别名) case_medical_insurance(是否医保) case_infectivity(传染性) case_fre_occ_population(易感人群) case_related_symptoms(相关症状) case_dep_main(科室1) case_dep_sub(科室2) ...
es_alias_manager.sh
本文件是针对apache的httpd-2.2.15.tar.gz源码包实现apache动态虚拟主机的配置,以及如何通过mod_vhost_alias.c增加模块mod_vhost_alias.so激活模块,使动态虚拟主机运行,以及实现了用户认证。
ALIAS_NA.RSA
这个想法是最终允许元编程与Python装饰器一起使用,而不必过多使用alias_method等。 换句话说,保留super /继承/方法重写。 尽管它是非常非常具有实验性的,并且有一些警告,但是它大部分都可以工作。 与创建的方法...
alias_manager是一个程序,可以帮助您管理别名 该程序已经在Ubuntu(20.04),Debian(10),Fedora(32),CentOS(8)和Arch Linux上进行了深入测试(包括单元测试)。 目录 描述 “别名管理器”是一个bash脚本,...
FlycoRoundView A library helps Android built-in views easy and convenient to set round rectangle background and accordingly related shape resources can be reduced. 一个扩展原生控件支持圆角矩形框背景的...
A variable in the event chain (listener-a) A - Static variable in class java.awt.PageAttributes.MediaType The MediaType instance for Engineering A, 8 1/2 x 11 in. A - Static variable in class javax....
StartActivity类-调用presenter方法 public class StartActivity extends AppCompatActivity implements StartView { Button btnNewGame; private StartPresenter presenter; @Override protected void ...
总结SQL中alias别名的应用,还有Linux中alias别名的应用的比较
Alias建模理论1--数学几何系统.pdf
简单分析了linux下system函数的相关内容,具体内容如下 int __libc_system (const char *line) { if (line == NULL) /* Check that we have a command processor available....weak_alias (__libc_system,
VITON-HD — Official PyTorch Model alias_final.pth
- group list_user_comments <topic_id> <user_alias> - group list_user_topics - group rec_topic <topic_id> - group remove_comment <topic_id> <comment_id> <reason> - group remove_commented_topic ...
这个工程是使用activity-alias创建应用的别名的Deom,有需要的可以下载
SELECT column_name AS column_alias FROM table_name --对表名进行重命名 SELECT column_name FROM table_name AS table_alias BETWEEN...AND... 在某个区间 SELECT column_name(s) FROM table_name WHERE ...
NULL 博文链接:https://zhengdl126.iteye.com/blog/417281
Autodesk Alias Automotive 2013