Have you ever wanted to easily log all variables? Now you can by using some advanced Ruby concepts as shown in this episode.
# models/product.rb
logger.debug_variables(binding)
# config/initializers/logger_additions.rb
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
通过使用一些高级Ruby概念,可以轻松地记录所有变量。本篇介绍了一种方法,可以在Rails应用程序中利用logger来输出当前作用域内的所有局部变量和实例变量。
548

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



