122

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

ActiveRecord::Relation:0x10355d1c0

What should I do?

2
  • In case you didn't see it (since you accepted an answer posted just before mine), do note that the debug() function works exactly like print_r() in PHP.
    – Andrew
    Jan 28, 2011 at 19:36
  • 1
    Just for anyone coming to this page later on debug() is outdated an not included as a function anymore. Won't work. (Credit to stackoverflow.com/users/231309/irongaze-com for pointing this out further down the page.)
    – kingsfoil
    Aug 21, 2014 at 19:21

9 Answers 9

246

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
#=> #<User:0x423270c @name="John Smith", @age=30>
puts user.to_yaml
#=> --- !ruby/object:User
#=> age: 30
#=> name: John Smith
1
  • 8
    I've found that some YAML outputs of records display more data (metadata, perhaps?) than I care to see. If I'm looking for the YAML version of a record I'll use y record_name.attributes. #y is an alias for to_yaml.
    – Tass
    Jan 14, 2016 at 15:16
9

define the to_s method in your model. For example

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.

3
  • What if you don't know what the variables it contains are?
    – cjm2671
    Jan 28, 2011 at 15:38
  • Can you be more specific? Are you saying, what if a variable is an array or hash? Their #to_s would take care of that. Jan 28, 2011 at 15:42
  • This is not phrased correctly. puts my_model_instance will not call to_s. You will have to explicitly do that: puts my_model_instance.to_s Feb 9, 2018 at 18:26
8

I'm using the awesome_print gem

So you just have to type :

ap @var
4
  • 2
    I'm not an advocate of installing a gem for something so simple, but I do use Awesome Print regularly.
    – Tass
    Jan 14, 2016 at 15:10
  • This is really good, what I eventually did in my code is this: Rails.logger.ap someObject May 22, 2017 at 12:07
  • gem install awesome_print (no edits needed to Gemfile)
    – Jay
    Dec 7, 2017 at 21:35
  • gem super_awesome_print enhances ap. Stands out more in iTerm at least
    – Greg
    May 28, 2021 at 16:19
7

In Rails you can print the result in the View by using the 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

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
0
6

pp does the job too, no gem requiring is required.

@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)>,

You can also print two instances of an object:

 pp( Accrual.first , Accrual.second)
`
`
`
3

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

To have full control over what's printed you could redefine to_s or inspect methods. Or create your own one to not confuse future devs too much.

  class Something < ApplicationRecord

    def to_s
      attributes.map{ |k, v| { k => v.to_s } }.inject(:merge)
    end

  end

This will apply a method (i.e. to_s) to all attributes. This example will get rid of the ugly BigDecimals.

You can also redefine a handful of attributes only:

  def to_s
    attributes.merge({ my_attribute: my_attribute.to_s })
  end

You can also create a mix of the two or somehow add associations.

3

.inspect is what you're looking for, it's way easier IMO than .to_yaml!

user = User.new
user.name = "will"
user.email = "will@example.com"

user.inspect
#<name: "will", email: "will@example.com">
2

instance.attributes prints the whole hash inside rails console.

-4

You need to use debug(@var). It's exactly like "print_r".

1
  • 5
    This is not a thing, at least on Ruby 1.9.x - NoMethodError: undefined method `debug' for main:Object Jan 20, 2014 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.