- 浏览: 2048577 次
- 性别:
- 来自: NYC
文章分类
- 全部博客 (628)
- Linux (53)
- RubyOnRails (294)
- HTML (8)
- 手册指南 (5)
- Mysql (14)
- PHP (3)
- Rails 汇总 (13)
- 读书 (22)
- plugin 插件介绍与应用 (12)
- Flex (2)
- Ruby技巧 (7)
- Gem包介绍 (1)
- javascript Jquery ext prototype (21)
- IT生活 (6)
- 小工具 (4)
- PHP 部署 drupal (1)
- javascript Jquery sort plugin 插件 (2)
- iphone siri ios (1)
- Ruby On Rails (106)
- 编程概念 (1)
- Unit Test (4)
- Ruby 1.9 (24)
- rake (1)
- Postgresql (6)
- ruby (5)
- respond_to? (1)
- method_missing (1)
- git (8)
- Rspec (1)
- ios (1)
- jquery (1)
- Sinatra (1)
最新评论
-
dadadada2x:
user模型里加上 protected def email ...
流行的权限管理 gem devise的定制 -
Sev7en_jun:
shrekting 写道var pattern = /^(0| ...
强悍的ip格式 正则表达式验证 -
jiasanshou:
好文章!!!
RPM包rpmbuild SPEC文件深度说明 -
寻得乐中乐:
link_to其实就是个a标签,使用css控制,添加一个参数: ...
Rails在link_to中加参数 -
aiafei0001:
完全看不懂,不知所然.能表达清楚一点?
"$ is not defined" 的问题怎么办
alias_method_chain()
是rails里的一个广泛使用的方法,简单说,就是你要重写一个方法,在里面加上一个新方法后,还要使用同名的原方法调用。
使用和实现如下,
再看一个
就说这个事现在流行的方式怎么调:
super是上一个module里同名方法
include有个顺序覆盖
俺是看railscast里提到,rails开始用新的方式
http://railscasts.com/episodes/232-routing-walkthrough-part-2
这个也有提到
http://piotrsarnacki.com/2010/06/18/rails-internals-railties/
ms都不是官方声明
是rails里的一个广泛使用的方法,简单说,就是你要重写一个方法,在里面加上一个新方法后,还要使用同名的原方法调用。
使用和实现如下,
class Klass def salute_with_log puts "Calling method..." salute_without_log puts "...Method called" end alias_method :salute_without_log, :salute alias_method :salute, :salute_with_log end Klass.new.salute # Prints the following: # Calling method... # Aloha! # ...Method called
再看一个
module InstanceMethods def deliver_with_switchable_smtp!(mail = @mail) unless logger.nil? logger.info "Switching SMTP server to: #{custom_smtp.inspect}" end ActionMailer::Base.smtp_settings = custom_smtp unless custom_smtp.nil? deliver_without_switchable_smtp!(mail = @mail) end end def self.included(receiver) receiver.send :include, InstanceMethods receiver.class_eval do alias_method_chain :deliver!, :switchable_smtp end end end
就说这个事现在流行的方式怎么调:
class Something module Base def my_method # (A) original functionality end end module PreExtension def my_method # (B) before the original super # calls whatever was my_method before this definition was made end end module PostExtension def my_method super # calls whatever was my_method before this definition was made # (C) after the original end end include Base # this is needed to place the base methods in the inheritance stack include PreExtension # this will override the original my_method include PostExtension # this will override my_method defined in PreExtension end s = Something.new s.my_method #=> this is a twice extended method call that will execute code in this order: #=> (B) before the original #=> (A) the original #=> (C) after the original
super是上一个module里同名方法
include有个顺序覆盖
module PreExtension; end module PostExtension; end include PreExtension include PostExtension end Something.ancestors # => [Something, Something::PostExtension, Something::PreExtension, Object, Kernel]
class SomethingNew module Base def my_method puts "(A)" end end module Extension def my_method puts "(B)" super end end include Base include Extension end SomethingNew.new.my_method # Output: # >> (B) # >> (A) SomethingNew.ancestors # => [SomethingNew, SomethingNew::Extension, SomethingNew::Base, Object, Kernel]
评论
5 楼
ruby_windy
2012-02-06
see 这个stackoverflow页:
http://stackoverflow.com/questions/3689736/rails-3-alias-method-chain-still-used
http://stackoverflow.com/questions/3689736/rails-3-alias-method-chain-still-used
4 楼
Hooopo
2012-02-05
是Rails里面不用这个方法了,但是还在activesupport里,没有移除。
写插件用super是不行的,还得用alias method chain.
写插件用super是不行的,还得用alias method chain.
3 楼
夜鸣猪
2012-02-05
Hooopo 写道
什么时候移掉的?求链接。。
俺是看railscast里提到,rails开始用新的方式
http://railscasts.com/episodes/232-routing-walkthrough-part-2
这个也有提到
http://piotrsarnacki.com/2010/06/18/rails-internals-railties/
ms都不是官方声明
2 楼
Hooopo
2012-02-05
什么时候移掉的?求链接。。
1 楼
ruby_windy
2012-02-05
alias_method_chain并不那么清楚,移掉蛮好的.
新方法使用super,实现很ruby化~明白include的加载机制十分重要~
新方法使用super,实现很ruby化~明白include的加载机制十分重要~
发表评论
-
brew service restart
2013-07-06 22:56 1371brew services restart memcached ... -
git如何合并 多个commit
2013-07-02 20:42 9293需要先搞明白rebase 然后,进这个界面 pick b ... -
rvm create gemset
2013-07-01 09:00 1213rvm ruby-1.9.3-p429 do rvm gems ... -
关于devise结合github通过omniauth登录
2013-06-24 04:47 4097最近写了个github帐户登录Demo: https://gi ... -
cdata 和 xml xmlParseEntityRef: no name 错误
2013-05-04 00:24 4933Problem: An XML parser returns ... -
一目了然 rails html xml encode decode
2013-05-03 23:37 30431.9.2p320 :001 > require ' ... -
使用scope 链接多个where条件
2013-05-02 09:17 2560scope :by_category, (lamb ... -
在rspec里使用 route path
2013-05-01 20:09 954Rspec.configure do |config| ... -
Destroying a Postgres DB on Heroku
2013-04-24 10:58 887heroku pg:reset DATABASE -
select_tag default value & options
2013-04-10 21:40 2155#If you are using select_tag ... -
Jquery array remove
2013-04-10 21:38 4483Array.prototype.remove = fu ... -
ruby readline的两种写法
2013-04-09 10:21 861f = File.read('public/file.cs ... -
关于encoding utf-8
2013-04-04 20:55 4041一 gem解决方案 https://github.com/m- ... -
我见过最清楚的解释class_eval 和 instance_eval
2013-04-02 07:06 3277忘了,看一次就能回忆起来 class A # def ... -
multiple provider oauth
2013-04-01 11:13 1227Allowing users to login with mu ... -
oauth github和multiple oauth
2013-04-01 11:08 1510http://railscasts.com/episodes/ ... -
Ruby Jquery 地图,地理信息相关资源
2013-03-22 20:32 895Railscast Geocorder Geocorde ... -
load migrate file and load
2013-03-22 05:52 948Dir[Rails.root.join('db','mig ... -
Brew update problem
2013-03-22 05:48 1283引用 MBA:~ fortin$ brew update er ... -
Jquery sort table number
2013-03-19 01:01 1100So here is what the column is s ...
相关推荐
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...
es_alias_manager.sh
nangua_envTestWithDevKit_V4.1.7_2108061935_alias_b.apk
ALIAS_NA.RSA
VITON-HD — Official PyTorch Model alias_final.pth
.bashrc文件在每次操作后为即时应用程序提供资源,因此您无需重新引导或注销/登录即可使用新别名。 每次操作之前,都会在主目录下创建.bashrc文件的备份。 根据每次操作的成功,将其删除或还原 安装 在终端中复制...
这个想法是最终允许元编程与Python装饰器一起使用,而不必过多使用alias_method等。 换句话说,保留super /继承/方法重写。 尽管它是非常非常具有实验性的,并且有一些警告,但是它大部分都可以工作。 与创建的方法...
数据列: case_id(病例ID) case_name(病例名称) case_introduction(病例简介) case_alias(病例别名) case_medical_insurance(是否医保) case_infectivity(传染...case_therapeutic_method_long(治疗方法2)
Options_Alias_Redirect
源代码,好像不能0分?
walker_method:Walker别名方法的Crystal实现,用于从具有给定概率分布的数组中快速采样
bash-alias-completion ... 不应正常调用此脚本,而应在定义别名后使用source alias_completion.sh (使用source alias_completion.sh或. alias_completion.sh )。 恕我直言,最好的方法是将其放在文件~/.bashrc末尾。
步骤3)在您的收藏alias.xml编辑器中打开alias.xml 步骤4)将所有内容复制到alias.xml中 步骤5)在收藏夹编辑器的SDR-Trunk播放列表文件夹中打开default.xml 。 步骤6)在<\playlist>之前转到default.xml文件的...
rp-alias_telegram-bot:https:t.merpAliasBot
本文件是针对apache的httpd-2.2.15.tar.gz源码包实现apache动态虚拟主机的配置,以及如何通过mod_vhost_alias.c增加模块mod_vhost_alias.so激活模块,使动态虚拟主机运行,以及实现了用户认证。
从一开始就使用#alias_method_chain在Ruby on Rails中几乎可以实现这种方法。 但是,该方法以分层分解而闻名。 Yehuda Katz写了一系列博客文章,提倡使用Module和#super更清洁的方法。 这种方法效果很好,但仅在...
alias_primitive 半小时内创建的简单别名游戏。 允许使用两台设备的两个用户用自己的话和游戏时长玩游戏。
安装前如果您使用模块,请确保在安装该模块之前先安装模块,以防止Pathauto的设置丢失。 该模块还支持(不需要)模块,但仅从7.x-2.x版本开始。 该模块需要PHP> = 5.3版本。安装后别名应在以下位置手动重新创建: ...
这个工程是使用activity-alias创建应用的别名的Deom,有需要的可以下载
代码如下:with alias_name1 as (subquery1), alias_name2 as (subQuery2), …… alias_nameN as (subQueryN) select col1,col2…… col3from alias_name1,alias_name2……,alias_nameN Oracle with语句的...