我无法理解此代码中 alias_method 的用途
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
alias_method :configure_connection_without_interval, :configure_connection
define_method :configure_connection do
configure_connection_without_interval
execute('SET intervalstyle = iso_8601', 'SCHEMA')
end
end
Run Code Online (Sandbox Code Playgroud)
他们调用的第 4 行的目的是什么configure_connection_without_interval——这不就是自称吗?
下面的代码对我有用,但我不完全知道我在做什么,我担心它以后会产生错误
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
define_method :configure_connection do
execute('SET intervalstyle = iso_8601', 'SCHEMA')
end
end
Run Code Online (Sandbox Code Playgroud)
alias_method 在重新定义方法之前立即在该行上操作,保留该给定名称下的旧方法。
这是一种常见的 Ruby 技术,用于捕获特定方法的版本并将其包装在另一个版本中。当您直接修补类而不是子类时,您通常会被迫这样做。
| 归档时间: |
|
| 查看次数: |
33 次 |
| 最近记录: |