如何在Rails中打印出对象的内容以便于调试?

cjm*_*671 91 ruby ruby-on-rails

我想我正试图让PHP等价print_r()(打印人类可读); 目前原始产量是:

ActiveRecord::Relation:0x10355d1c0
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

jer*_*ith 195

我一般先试试.inspect,如果那不能给我我想要的东西,我会转而去.to_yaml.

class User
  attr_accessor :name, :age
end

user = User.new
user.name = "John Smith"
user.age = 30

puts user.inspect
#=> #<User:0x423270c @name="John Smith", @age=30>
puts user.to_yaml
#=> --- !ruby/object:User
#=> age: 30
#=> name: John Smith
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • 我发现记录的一些YAML输出显示的数据(元数据,可能是?)比我想看到的要多.如果我正在寻找记录的YAML版本,我将使用`y record_name.attributes`.`#y`是`to_yaml`的别名. (4认同)

Chr*_*det 8

在模型中定义to_s方法.例如

class Person < ActiveRecord::Base
  def to_s
    "Name:#{self.name} Age:#{self.age} Weight: #{self.weight}"
  end
end
Run Code Online (Sandbox Code Playgroud)

然后当你用#puts打印它时,它将显示带有这些变量的字符串.


小智 7

我正在使用 awesome_print gem

所以你只需要输入:

ap @var
Run Code Online (Sandbox Code Playgroud)

  • 我不提倡为这么简单的事情安装 gem,但我确实经常使用 Awesome Print。 (2认同)

zee*_*zee 6

pp 也可以完成这项工作,不需要任何宝石。

@a = Accrual.first ; pp @a

#<Accrual:0x007ff521e5ba50
 id: 4,
 year: 2018,
 Jan: #<BigDecimal:7ff521e58f08,'0.11E2',9(27)>,
 Feb: #<BigDecimal:7ff521e585d0,'0.88E2',9(27)>,
 Mar: #<BigDecimal:7ff521e58030,'0.0',9(27)>,
 Apr: #<BigDecimal:7ff521e53698,'0.88E2',9(27)>,
 May: #<BigDecimal:7ff521e52fb8,'0.8E1',9(27)>,
 June: #<BigDecimal:7ff521e52900,'0.8E1',9(27)>,
 July: #<BigDecimal:7ff521e51ff0,'0.8E1',9(27)>,
 Aug: #<BigDecimal:7ff521e51bb8,'0.88E2',9(27)>,
 Sep: #<BigDecimal:7ff521e512f8,'0.88E2',9(27)>,
 Oct: #<BigDecimal:7ff521e506c8,'0.0',9(27)>,
 Nov: #<BigDecimal:7ff521e43d38,'0.888E3',9(27)>,
 Dec: #<BigDecimal:7ff521e43478,'0.0',9(27)>,
Run Code Online (Sandbox Code Playgroud)

您还可以打印对象的两个实例:

 pp( Accrual.first , Accrual.second)
`
`
`
Run Code Online (Sandbox Code Playgroud)


Pap*_*nho 5

在Rails中,您可以使用调试'Helper ActionView :: Helpers :: DebugHelper在View中打印结果

#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
Run Code Online (Sandbox Code Playgroud)

结果(在浏览器中)

- !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
Run Code Online (Sandbox Code Playgroud)