How to get nice formatting in the Rails console

rails console formatting
rails console pretty print json
rails console pretty print hash
rails console colors
pry-rails
rails console format hash
hirb gem
rails 6 console

I want to get something like this to look nice:

>> ProductColor.all
=> [#<ProductColor id: 1, name: "White", internal_name: "White", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 2, name: "Ivory", internal_name: "Ivory", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 3, name: "Blue", internal_name: "Light Blue", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 4, name: "Green", internal_name: "Green", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">]

This doesn't work:

>> ProductColor.all.inspect
=> "[#<ProductColor id: 1, name: \"White\", internal_name: \"White\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 2, name: \"Ivory\", internal_name: \"Ivory\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 3, name: \"Blue\", internal_name: \"Light Blue\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 4, name: \"Green\", internal_name: \"Green\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">]"

And neither does this:

>> ProductColor.all.to_yaml
=> "--- \n- !ruby/object:ProductColor \n  attributes: \n    name: White\n    created_at: 2009-06-10 04:02:44\n    updated_at: 2009-06-10 04:02:44\n    id: \"1\"\n    internal_name: White\n  attributes_cache: {}\n\n- !ruby/object:ProductColor \n  attributes: \n    name: Ivory\n    created_at: 2009-06-10 04:02:44\n    updated_at: 2009-06-10 04:02:44\n    id: \"2\"\n    internal_name: Ivory\n  attributes_cache: {}\n\n- !ruby/object:ProductColor \n  attributes: \n    name: Blue\n    created_at: 2009-06-10 04:02:44\n    updated_at: 2009-06-10 04:02:44\n    id: \"3\"\n    internal_name: Light Blue\n  attributes_cache: {}\n\n- !ruby/object:ProductColor \n  attributes: \n    name: Green\n    created_at: 2009-06-10 04:02:44\n    updated_at: 2009-06-10 04:02:44\n    id: \"4\"\n    internal_name: Green\n  attributes_cache: {}\n\n"

Thoughts?

The y method is a handy way to get some pretty YAML output.

y ProductColor.all

Assuming you are in script/console

As jordanpg commented, this answer is outdated. For Rails 3.2+ you need to execute the following code before you can get the y method to work:

YAML::ENGINE.yamler = 'syck'

From ruby-docs

In older Ruby versions, ie. <= 1.9, Syck is still provided, however it was completely removed with the release of Ruby 2.0.0.

For rails 4/ruby 2 you could use just

puts object.to_yaml

Making the rails console output a little more pretty, pretty command in MongoDB console that was formatting the output a little more eye friendly. But not sure if there is something similar in Rails or� There are couple of ways to get pretty output - list or a table output. For list output formatting use pry, yaml or awesome_print. And for table output formatting use hirb or irbtools. Pry. Add pry to Gemfile and bundle. Then simply type pry in rails console:

You should try hirb. It's a gem made to to pretty format objects in the ruby console. Your script/console session would look like this:

>> require 'hirb'
=> true
>> Hirb.enable
=> true
>> ProductColor.first
+----+-------+---------------+---------------------+---------------------+
| id | name  | internal_name | created_at          | updated_at          |
+----+-------+---------------+---------------------+---------------------+
| 1  | White | White         | 2009-06-10 04:02:44 | 2009-06-10 04:02:44 |
+----+-------+---------------+---------------------+---------------------+
1 row in set
=> true

You can learn more about hirb at its homepage.

Rails console: six ways to get pretty output, And for table output formatting use hirb or irbtools . Pry. Add pry to Gemfile and bundle. Then simply type pry in rails console: > pry. I love using the Rails console (irb with your Rails app loaded) to try out methods & play around with things. Helpers included! You can use helpers from the console with helper.method_name .

Awesome print is nice too if you want an object indented. Something like:

$ rails console
rails> require "awesome_print"
rails> ap Account.all(:limit => 2)
[
    [0] #<Account:0x1033220b8> {
                     :id => 1,
                :user_id => 5,
            :assigned_to => 7,
                   :name => "Hayes-DuBuque",
                 :access => "Public",
                :website => "http://www.hayesdubuque.com",
        :toll_free_phone => "1-800-932-6571",
                  :phone => "(111)549-5002",
                    :fax => "(349)415-2266",
             :deleted_at => nil,
             :created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
             :updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
                  :email => "info@hayesdubuque.com",
        :background_info => nil
    },
    [1] #<Account:0x103321ff0> {
                     :id => 2,
                :user_id => 4,
            :assigned_to => 4,
                   :name => "Ziemann-Streich",
                 :access => "Public",
                :website => "http://www.ziemannstreich.com",
        :toll_free_phone => "1-800-871-0619",
                  :phone => "(042)056-1534",
                    :fax => "(106)017-8792",
             :deleted_at => nil,
             :created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
             :updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
                  :email => "info@ziemannstreich.com",
        :background_info => nil
    }
]

To integrate it by default with your irb/rails/pry console, add to your ~/.irbrc or ~/.pryrc file:

require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc

Rails console: five ways to get pretty output : rails, Ruby's pretty-print library pp handles formatted printing (you might need to require 'pp' ). Pry will give you a readable output, too. If it's already used in your project,� Clearing the Console. To get a clean slate in your console, use Command + K on the Mac. You can also use Ctrl + L to clear the screen, which works on both Mac and Linux. If you’re on Windows, the only way we’re aware of to clear the console is by typing system ('cls').

>> puts ProductColor.all.to_yaml

Simply works fine!

Source: https://stackoverflow.com/a/4830096

Make your Rails console (and irb) output better readable, A description of the different ways to output content in Ruby. that comes to mind , but also because it shows our result in a nicely formatted way. and you can even make it your default formatter for irb, Rails console and pry. 1.4 rails console. The console command lets you interact with your Rails application from the command line. On the underside, rails console uses IRB, so if you’ve ever used it, you’ll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.

May also be noted that you can use:

j ProductColor.all.inspect

to output in Json format rather than Yaml

puts, prints and more — printing to the console in Ruby…, As a Rails developer, you'll end up spending a lot of time in the Rails console . To get a clean slate in your console, use Command + K on the Mac. it would be nice to avoid typing movie. every time you want to message� Leverage the powerful Rails console tool to run Ruby scripts, access the full Rails environment, access the database and more.

Rails Console Shortcuts, Tips, and Tricks, This will go to rails database console. rails db pretty print your query output How to get nice formatting in the Rails console, It's a gem made to to pretty format � 1.4 rails console. The console command lets you interact with your Rails application from the command line. On the underside, rails console uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.

Rails console display all models, Rails console is a great tool when you are experimenting with your code, but if we have a lot of data Tagged with rails Hirb is a 'mini view framework' that formats output console according to its type. So you go from� In order to accomplish this, we setup a bash script which is used to open a Rails console. In this bash script we choose to use a lesser known feature that Rails offers, DATABASE_URL. If you set the DATABASE_URL variable in your environment, Rails will use it to connect to your database rather than reading from your database.yml file. This allows us to override our database configs when we need to.

How to simplify reading your Rails console, Apr 24, 2017 - irb - How to get nice formatting in the Rails console - Stack Overflow. Setting up automated code formatting for Rails using NodeJS 28 Aug 2017 Why? As one of the Rails projects I’ve been working grew in size in terms of the number of lines of code as well as the number of people contributing code, it became challenging to maintain consistency in code quality and style.

Comments
  • this should be marked as the right answer since it's built in, can be used immediately, and most of all, simple.
  • This answer is outdated. See: stackoverflow.com/questions/11571801/… To make this work, you have to first execute YAML::ENGINE.yamler = 'syck'.
  • Its now YAML::ENGINE.yamler = 'psych'
  • This is similar to ryanb >> ProductColor.all >> y _
  • like @botbot said above, this is the best answer since it applies to situations where you ahve no access to an .irbrc, other console tools, or other configurations of the console (like, being a contracted dev with limited access to a production container/server)
  • ryanb's answer is basically what I was looking for, but this is too cool not to accept.
  • While not an answer to the original question, it might be pointing out that you can add the hirb stuff to your ~/.irbrc so you don't have to require and enable it each time.
  • This gem is outdated now .
  • Is there a way to "easily" sort the columns of the output? I would love to force the column id to be first and updated_at & created_at at the end (if you add columns after the first migration the updated_at & created_at columns will not be at the end)
  • Can I use this gem with Rails 4 or 5 ?? I've found the following note in the github page: NOTE: awesome_print v1.2.0 is the last release supporting Ruby versions prior to v1.9.3 and Rails versions prior to v3.0. The upcoming awesome_print v2.0 will require Ruby v1.9.3 or later and Rails v3.0 or later. Does this mean that the gem is outdated with these versions and cause conflicts??
  • This works great! I couldn't get the other top voted answers to work... I guess because I am using ActiveResource (API resources)
  • this can fail depending on version of JSON/ruby, and the nice formatting may be needed in an environment where one cant have nice things