【问题标题】:Is there a print_r or var_dump equivalent in Ruby / Ruby on Rails?Ruby / Ruby on Rails 中是否有等效的 print_r 或 var_dump?
【发布时间】:2009-05-24 17:59:22
【问题描述】:

出于调试原因,我正在寻找一种转储对象结构的方法,类似于 PHP 函数 print_rvar_dump

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    任何对象的.inspect 方法的格式都应该正确显示,就这样吧..

    <%= theobject.inspect %>
    

    .methods 方法也可能有用:

    <%= theobject.methods.inspect %>
    

    根据数据将其放入&lt;pre&gt; 标记可能会有所帮助

    【讨论】:

    • 只是为那些在控制台中寻找更整洁格式的人节省时间:puts theobject.inspect.gsub(",", "\n")
    【解决方案2】:

    在视图中:

    include DebugHelper
    
    ...your code...
    
    debug(object)
    

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

    puts YAML::dump(object)
    

    Source

    【讨论】:

    • DebugHelper 的调试(对象)引发未定义的方法 `DebugHelper's' :)
    【解决方案3】:

    在视图中,您可以使用&lt;%= debug(yourobject) %&gt;,它将生成数据的 YAML 视图。如果你想在你的日志中添加一些东西,你应该使用logger.debug yourobject.inspect

    【讨论】:

      【解决方案4】:

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

      它非常有用!

      【讨论】:

        【解决方案5】:

        您可以使用puts some_variable.inspect。或者更短的版本:p some_variable。对于更漂亮的输出,您可以使用awesome_print gem

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

            以前的答案很好,但如果你不想使用控制台(终端),在 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
            

            【讨论】:

              【解决方案8】:

              我用这个:)

              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
              

              【讨论】:

                【解决方案9】:

                最近我正在使用 awesome_printap 方法,该方法在控制台和视图中都可以使用。

                如果您需要在视觉上扫描StringNumeric 对象,特定类型的彩色输出确实会有所不同(尽管我必须稍微调整一下样式表才能获得精美的外观)

                【讨论】:

                  【解决方案10】:

                  最近我成为了PRY 的粉丝,我发现它非常适合检查变量、调试运行代码和检查外部代码。作为这个特定问题的答案,这可能有点矫枉过正。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-08-05
                    • 1970-01-01
                    • 2013-09-26
                    • 1970-01-01
                    相关资源
                    最近更新 更多