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

时间:2021-11-12 02:37:47

I think I'm trying to get the PHP equivalent of print_r() (print human-readable); at present the raw output is:

我想我想要的是PHP的print_r()(打印人类可读);目前的原始产出是:

ActiveRecord::Relation:0x10355d1c0

What should I do?

我应该做什么?

7 个解决方案

#1


166  

I generally first try .inspect, if that doesn't give me what I want, I'll switch to .to_yaml.

我通常首先尝试。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

Hope that helps.

希望有帮助。

#2


8  

define the to_s method in your model. For example

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

class Person < ActiveRecord::Base
  def to_s
    "Name:#{self.name} Age:#{self.age} Weight: #{self.weight}"
  end
end

Then when you go to print it with #puts it will display that string with those variables.

然后,当你用#put打印它时,它会显示带有这些变量的字符串。

#3


4  

In Rails you can print the result in the View by using the debug' Helper ActionView::Helpers::DebugHelper

在Rails中,您可以使用debug' Helper ActionView::Helper::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

results (in browser)

结果(在浏览器)

- !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

#4


3  

I'm using the awesome_print gem

我用的是awesome_print gem

So you just have to type :

你只需要输入

ap @var

#5


1  

inspect is great but sometimes not good enough. E.g. BigDecimal prints like this: #<BigDecimal:7ff49f5478b0,'0.1E2',9(18)>.

检查很好,但有时不够好。像这样的BigDecimal打印:# <7ff49f5478b0>

<>


  

<>


  

#1


166  

I generally first try .inspect, if that doesn't give me what I want, I'll switch to .to_yaml.

我通常首先尝试。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

Hope that helps.

希望有帮助。

#2


8  

define the to_s method in your model. For example

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

class Person < ActiveRecord::Base
  def to_s
    "Name:#{self.name} Age:#{self.age} Weight: #{self.weight}"
  end
end

Then when you go to print it with #puts it will display that string with those variables.

然后,当你用#put打印它时,它会显示带有这些变量的字符串。

#3


4  

In Rails you can print the result in the View by using the debug' Helper ActionView::Helpers::DebugHelper

在Rails中,您可以使用debug' Helper ActionView::Helper::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

results (in browser)

结果(在浏览器)

- !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

#4


3  

I'm using the awesome_print gem

我用的是awesome_print gem

So you just have to type :

你只需要输入

ap @var

#5


1  

inspect is great but sometimes not good enough. E.g. BigDecimal prints like this: #<BigDecimal:7ff49f5478b0,'0.1E2',9(18)>.

检查很好,但有时不够好。像这样的BigDecimal打印:# <7ff49f5478b0>

<>


  

<>