我们可能会经常使用logger.debug来debug一些变量
[code]
logger.debug "Year: #{year} Month #{month}"
[/code]
麻烦,不是么,我们可以添加一个config/initializers/logger_additions.rb
[code]
logger = ActiveRecord::Base.logger
def logger.debug_variables(bind)
vars = eval('local_variables + instance_variables', bind) # 得到所有本地变量和实例变量
vars.each do |var|
debug "#{var} = #{eval(var, bind).inspect}"
end
end
[/code]
然后,我们可以这样做来debug变量了
[code]
# models/product.rb
logger.debug_variables(binding)
[/code]
[code]
logger.debug "Year: #{year} Month #{month}"
[/code]
麻烦,不是么,我们可以添加一个config/initializers/logger_additions.rb
[code]
logger = ActiveRecord::Base.logger
def logger.debug_variables(bind)
vars = eval('local_variables + instance_variables', bind) # 得到所有本地变量和实例变量
vars.each do |var|
debug "#{var} = #{eval(var, bind).inspect}"
end
end
[/code]
然后,我们可以这样做来debug变量了
[code]
# models/product.rb
logger.debug_variables(binding)
[/code]
简化Rails Debugging
本文介绍了一种在Ruby on Rails中简化变量调试的方法。通过在config/initializers中添加自定义logger,可以方便地打印出所有本地变量和实例变量的值,从而提高开发效率。
4317

被折叠的 条评论
为什么被折叠?



