How do I print out the contents of an object in Rails for easy debugging?

前端 未结 8 1156
感情败类
感情败类 2020-12-04 18:32

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

ActiveRecord::Relation:0x10355d         


        
8条回答
  •  甜味超标
    2020-12-04 19:19

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

    class User
      attr_accessor :name, :age
    end
    
    user = User.new
    user.name = "John Smith"
    user.age = 30
    
    puts user.inspect
    #=> #
    puts user.to_yaml
    #=> --- !ruby/object:User
    #=> age: 30
    #=> name: John Smith
    

    Hope that helps.

提交回复
热议问题