【发布时间】:2009-05-24 17:59:22
【问题描述】:
出于调试原因,我正在寻找一种转储对象结构的方法,类似于 PHP 函数 print_r 和 var_dump。
【问题讨论】:
标签: ruby-on-rails ruby
出于调试原因,我正在寻找一种转储对象结构的方法,类似于 PHP 函数 print_r 和 var_dump。
【问题讨论】:
标签: ruby-on-rails ruby
任何对象的.inspect 方法的格式都应该正确显示,就这样吧..
<%= theobject.inspect %>
.methods 方法也可能有用:
<%= theobject.methods.inspect %>
根据数据将其放入<pre> 标记可能会有所帮助
【讨论】:
puts theobject.inspect.gsub(",", "\n")
在视图中:
include DebugHelper
...your code...
debug(object)
在控制器、模型和其他代码中:
puts YAML::dump(object)
【讨论】:
在视图中,您可以使用<%= debug(yourobject) %>,它将生成数据的 YAML 视图。如果你想在你的日志中添加一些东西,你应该使用logger.debug yourobject.inspect。
【讨论】:
您还可以在 Rails 控制台下使用 YAML::dump 简写(y):
>> y User.first
--- !ruby/object:User
attributes:
created_at: 2009-05-24 20:16:11.099441
updated_at: 2009-05-26 22:46:29.501245
current_login_ip: 127.0.0.1
id: "1"
current_login_at: 2009-05-24 20:20:46.627254
login_count: "1"
last_login_ip:
last_login_at:
login: admin
attributes_cache: {}
=> nil
>>
如果您只想预览一些字符串内容,请尝试使用 raise(例如在模型、控制器或其他无法访问的地方)。您可以免费获得回溯:)
>> raise Rails.root
RuntimeError: /home/marcin/work/github/project1
from (irb):17
>>
我也非常鼓励您尝试 ruby-debug:
它非常有用!
【讨论】:
您可以使用puts some_variable.inspect。或者更短的版本:p some_variable。对于更漂亮的输出,您可以使用awesome_print gem。
【讨论】:
如果您只想将相关数据显示到标准输出(如果您从命令行运行,则为终端输出),您可以使用p some_object。
【讨论】:
以前的答案很好,但如果你不想使用控制台(终端),在 Rails 中你可以使用调试的 Helper ActionView::Helpers::DebugHelper在视图中打印结果
#app/view/controllers/post_controller.rb
def index
@posts = Post.all
end
#app/view/posts/index.html.erb
<%= debug(@posts) %>
#start your server
rails -s
结果(在浏览器中)
- !ruby/object:Post
raw_attributes:
id: 2
title: My Second Post
body: Welcome! This is another example post
published_at: '2015-10-19 23:00:43.469520'
created_at: '2015-10-20 00:00:43.470739'
updated_at: '2015-10-20 00:00:43.470739'
attributes: !ruby/object:ActiveRecord::AttributeSet
attributes: !ruby/object:ActiveRecord::LazyAttributeHash
types: &5
id: &2 !ruby/object:ActiveRecord::Type::Integer
precision:
scale:
limit:
range: !ruby/range
begin: -2147483648
end: 2147483648
excl: true
title: &3 !ruby/object:ActiveRecord::Type::String
precision:
scale:
limit:
body: &4 !ruby/object:ActiveRecord::Type::Text
precision:
scale:
limit:
published_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
subtype: &1 !ruby/object:ActiveRecord::Type::DateTime
precision:
scale:
limit:
created_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
subtype: *1
updated_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
subtype: *1
【讨论】:
我用这个:)
require 'yaml'
module AppHelpers
module Debug
module VarDump
class << self
def dump(dump_object, file_path)
File.open file_path, "a+" do |log_file|
current_date = Time.new.to_s + "\n" + YAML::dump(dump_object) + "\n"
log_file.puts current_date
log_file.close
end
end
end
end
end
end
【讨论】:
最近我正在使用 awesome_print 的 ap 方法,该方法在控制台和视图中都可以使用。
如果您需要在视觉上扫描String 或Numeric 对象,特定类型的彩色输出确实会有所不同(尽管我必须稍微调整一下样式表才能获得精美的外观)
【讨论】:
最近我成为了PRY 的粉丝,我发现它非常适合检查变量、调试运行代码和检查外部代码。作为这个特定问题的答案,这可能有点矫枉过正。
【讨论】: