9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

hirb でテーブルを縦表示にするには

Last updated at Posted at 2015-05-23

hirb という gem を導入すると REPL 上で ActiveRecord オブジェクトをテーブル風に表示してくれて、とても見やすくなって便利。
ただ、大量のカラムがあるレコードを表示する際は、以下のようにカラムの値が省略されまくってしまい、逆に見えにくくなってしまう時がある。

pry 2.2.1-p85> event
+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+------+-----+
| id  | cou | star | end | dea | add | loc | acc | sea | min | rem | can | cro | rem | aaa | bbbb | cre | upda | del |
+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+------+-----+
| xxx | xxx | 2015 | 201 | 201 |   |   |   | 3   | 1   | 2   | 201 |     |     |     | 5eb5 | 201 | 2015 |     |
+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+------+-----+
1 row in set

で、このような時、 hirb が自動で縦表示に切り替えてくれる場合もあるんだけど、そうでない時に手動で縦表示にするには、以下のようにすればいいようだ。

pry 2.2.1-p85> extend Hirb::Console
main

pry 2.2.1-p85> table event, vertical: true
********************* 1. row *********************
            id: xxx
     course_id: xxx
    start_time: 2015-05-23 11:00:00 +0900
      end_time: 2015-05-23 12:15:00 +0900
      deadline: 2015-05-22
       address: XXXXX
      location: XXXXX
        access: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
           ...
1 row in set
true

追記 - 2016/04/07 19:55

.pryrc に以下のような関数を定義しておくと便利になった。

.pryrc

   begin
     require 'hirb'
   rescue LoadError
   end

   if defined? Hirb
     extend Hirb::Console
     def vtable(record, options = {}, *args)
       table record, { vertical: true }.merge(options), *args
     end
   end
   pry> vtable Hoge.last
   ****************** 1. row ******************
             id: 1
     created_at: 2016-03-28 19:00:07 +0900
     updated_at: 2016-03-28 19:00:07 +0900
     ...
   1 row in set
   => true
9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?