IRB和大变量?

时间:2010-06-03 12:55:59

标签: ruby console pretty-print irb

如何在irb提示符中很好地打印大变量?我有一个包含许多变量的变量,这些变量很长,打印输出变得很乱。如果我只想要没有值的变量名怎么办?或者,我可以在一个单独的行上打印每个,根据深度选项卡吗?

2 个答案:

答案 0 :(得分:4)

  

或者,我可以在一个单独的行上打印每个,根据深度选项卡吗?

使用pp(漂亮的印刷品):

require 'pp'
very_long_hash = Hash[(1..23).zip(20..42)]
pp very_long_hash
# Prints:
{1=>20,
 2=>21,
 3=>22,
 4=>23,
 5=>24,
 6=>25,
 7=>26,
 8=>27,
 9=>28,
 10=>29,
 11=>30,
 12=>31,
 13=>32,
 14=>33,
 15=>34,
 16=>35,
 17=>36,
 18=>37,
 19=>38,
 20=>39,
 21=>40,
 22=>41,
 23=>42}

答案 1 :(得分:2)

如果你想要的东西比“漂亮”的东西更棒,你可以使用"awesome" print。为了获得真正的间隔体验,请在您的IRb上撒上一些hirbal medicine

例如,Hirb将ActiveRecord对象(或几乎任何数据库访问库)呈现为实际的ASCII表:

+-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
| id  | created_at              | description | name              | namespace | predicate | value    |
+-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
| 907 | 2009-03-06 21:10:41 UTC |             | gem:tags=yaml     | gem       | tags      | yaml     |
| 906 | 2009-03-06 08:47:04 UTC |             | gem:tags=nomonkey | gem       | tags      | nomonkey |
| 905 | 2009-03-04 00:30:10 UTC |             | article:tags=ruby | article   | tags      | ruby     |
+-----+-------------------------+-------------+-------------------+-----------+-----------+----------+