IRB 和大变量?

发布于 2024-09-04 15:16:02 字数 116 浏览 5 评论 0原文

如何在 irb 提示符中很好地打印一个大变量?我有一个变量,其中包含许多很长的变量,并且打印输出变得一团糟。如果我只想要变量名而不想要它们的值怎么办?或者,我可以将每一个打印在单独的一行上,并根据深度进行制表符插入吗?

How can I print a large variable nicely in an irb prompt? I have a variable that contains many variables which are long and the printout becomes a mess to wade through. What if I just want the variable names without their values? Or, can I print each one on a separate line, tabbed-in depending on depth?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫人生路 2024-09-11 15:16:02

或者,我可以将每一个打印在单独的行上,并根据深度插入吗?

使用 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}

Or, can I print each one on a separate line, tabbed-in depending on depth?

Use pp (pretty print):

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}
穿越时光隧道 2024-09-11 15:16:02

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

例如,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     |
+-----+-------------------------+-------------+-------------------+-----------+-----------+----------+

If you want something that is even more awesome than "pretty" print, you can use "awesome" print. And for a truly spaced out experience, sprinkle some hirbal medicine on your IRb!

Hirb, for example, renders ActiveRecord objects (or pretty much any database access library) as actual ASCII tables:

+-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
| 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     |
+-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文