在Ruby / Ruby on Rails中是否有类似于print_r或var_dump的函数?

129

我正在寻找一种类似于 PHP 函数 print_rvar_dump 用于调试的方法,可以将一个对象的结构进行转储。

10个回答

147

.inspect方法可以正确格式化任何对象的显示,只需执行即可。

<%= theobject.inspect %>

.methods方法可能也会有用:

<%= theobject.methods.inspect %>

视数据情况而定,将其放入<pre>标签中可能会有所帮助。


3
只是一个节省时间的工具,适用于那些在控制台中寻找更整洁格式的人:puts theobject.inspect.gsub(",", "\n") - Gus

68

在视图中:

include DebugHelper

...your code...

debug(object)

在控制器、模型和其他代码中:

puts YAML::dump(object)

源代码


DebugHelper的debug(object)引发了未定义方法`DebugHelper's' :) - Arnold Roa

9
在视图中,您可以使用<%= debug(yourobject) %>,它将生成您的数据的 YAML 视图。如果您想在日志中查看某些内容,您应该使用logger.debug yourobject.inspect

7
您可以在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:

它非常有帮助!


7
你可以使用puts some_variable.inspect。或者更短的版本:p some_variable。为了更美观,你可以使用awesome_print gem

4

之前的答案很好,但如果您不想使用控制台(终端),在Rails中您可以通过使用debug的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

3
如果您只想在标准输出(如果您正在从命令行运行,则为终端输出)中显示相关数据,可以使用p some_object

0
我使用这个 :)
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

0

最近我成为了PRY的粉丝,我发现它非常适合用于检查变量、调试运行代码和检查外部代码。对于这个特定问题来说,它可能有点过头了。


0
最近我正在使用 awesome_printap 方法,它可以在控制台和视图中使用。
如果您需要视觉扫描 StringNumeric 对象,类型特定的彩色输出确实会有所不同(尽管我不得不微调我的样式表以获得精美的外观)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接