Skip to content

Instantly share code, notes, and snippets.

@kieetnvt
Created November 30, 2017 16:18
Show Gist options
  • Save kieetnvt/f508f946bcf9f0474ecd2b48381db14e to your computer and use it in GitHub Desktop.
Save kieetnvt/f508f946bcf9f0474ecd2b48381db14e to your computer and use it in GitHub Desktop.
Rails console: six ways to get pretty output
  1. use Pry command: in rails console:
> pry

you will redirected to pry console:

pry(main)
  1. use Yaml command: in rails console:
> y User.first.attributes
or
> puts User.last.attributes.to_yaml
  1. use Awesome print gem: add gem 'awesome_print' to Gemfile and in rails console:
> require 'awesome_print'
> ap User.first
  1. use Hirb gem: (show result as Mysql result) add gem 'hirb' to Gemfile and in rails console:
> require 'hirb'
> Hirb.enable
> Movie.select("title, year, genre,director").first
> 
+----+--------+------+-------------------+------------+
| id | title  | year | genre             | director   |
+----+--------+------+-------------------+------------+
|    | Batman | 1989 | Action, Adventure | Tim Burton |
+----+--------+------+-------------------+------------+
  1. Irbtools (like Hirb): Add gem 'irbtools' to Gemfile and in rails console:
> require 'irbtools'
> Movie.select("title, year, genre,director").first
>
┌────┬────────┬──────┬───────────────────┬────────────┐
│ id │ title  │ year │ genre             │ director   │
├────┼────────┼──────┼───────────────────┼────────────┤
│    ╎ Batman ╎ 1989 ╎ Action, Adventure ╎ Tim Burton │
└────┴────────┴──────┴───────────────────┴────────────┘
  1. Table print (like Hirb): Add gem 'table_print' to Gemfile and in rails console:
> tp Movie.first
> tp Movie.first, "title", "year", "genre", "director"
TITLE  | YEAR | GENRE             | DIRECTOR
-------|------|-------------------|-----------
Batman | 1989 | Action, Adventure | Tim Burton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment