ruby - 如何使用 Ruby 语言在不同的行中显示每个对象的属性以提高命令行的可读性?

标签 ruby linux command-line terminal

有没有我可以调用的方法或命令来使命令行中的对象更具可读性? 我可以展示这个的东西:

[6] pry(main)> job.subscription
=> #<Subscription id: 21, frequency: 0, user_id: 88, created_at: "2016-02-24 09:34:46", updated_at: "2016-02-24 09:34:47", customer_note_security: "I generally just leave the door wide open", customer_note_cleaning: "Please clean the cat too", merchant_id: nil, deleted_at: nil, repeat_reminder_sent_at: nil, store_id: nil, next_job_date: nil, subscription_application_id: nil, hourly_price: nil>

像这样

[6] pry(main)> job.subscription
=> #<Subscription id: 21,
frequency: 0, 
user_id: 88,
created_at: "2016-02-24 09:34:46", 
updated_at: "2016-02-24 09:34:47", 
customer_note_security: "I generally just leave the door wide open", 
customer_note_cleaning: "Please clean the cat too", 
merchant_id: nil, 
deleted_at: nil, 
repeat_reminder_sent_at: nil, 
store_id: nil, 
next_job_date: nil,
subscription_application_id: nil,
hourly_price: nil>

使用pp

[36] pry(main)> pp job
#<Job id: 23, subtotal: #<BigDecimal:7fbf644986c8,'0.0',9(18)>, hours: #<BigDecimal:7fbf644989c0,'0.0',9(18)>, subscription_id: 22, address_id: 37, state: "unpaid", date: "2016-02-27", time_preferences: "MyString", type: nil, region: "auckland", items: [], options: {}, created_at: "2016-02-24 09:59:52", updated_at: "2016-02-24 09:59:52", merchant_id: nil, notes: nil, deleted_at: nil, merchant_rate: nil, canceled: false, start_time: nil, end_time: nil, grand_total: #<BigDecimal:7fbf64498538,'0.0',9(18)>, redemption_code_id: nil, discount_total: #<BigDecimal:7fbf64498010,'0.0',9(27)>, payment_notification_sent_at: nil, last_payment_attempt_at: nil, store_id: nil, merchant_received_notification_at: nil, customer_balance: #<BigDecimal:7fbf5af0b3a8,'0.0',9(18)>, promotional_credit_applied: #<BigDecimal:7fbf64438278,'0.0',9(18)>, spawned_by_cron: false, initial_assignment_notification_received_at: nil>
=> #<Job id: 23, subtotal: #<BigDecimal:7fbf644986c8,'0.0',9(18)>, hours: #<BigDecimal:7fbf644989c0,'0.0',9(18)>, subscription_id: 22, address_id: 37, state: "unpaid", date: "2016-02-27", time_preferences: "MyString", type: nil, region: "auckland", items: [], options: {}, created_at: "2016-02-24 09:59:52", updated_at: "2016-02-24 09:59:52", merchant_id: nil, notes: nil, deleted_at: nil, merchant_rate: nil, canceled: false, start_time: nil, end_time: nil, grand_total: #<BigDecimal:7fbf64498538,'0.0',9(18)>, redemption_code_id: nil, discount_total: #<BigDecimal:7fbf64498010,'0.0',9(27)>, payment_notification_sent_at: nil, last_payment_attempt_at: nil, store_id: nil, merchant_received_notification_at: nil, customer_balance: #<BigDecimal:7fbf5af0b3a8,'0.0',9(18)>, promotional_credit_applied: #<BigDecimal:7fbf64438278,'0.0',9(18)>, spawned_by_cron: false, initial_assignment_notification_received_at: nil>
[37] pry(main)> 

最佳答案

如果你在 Rails 上 pretty-print 对象 attributes :

pp job.subscription.attributes

或类似的:

pp job.attributes

否则您可以将对象转换为 Hash 并使用 pp 漂亮地打印它。

关于ruby - 如何使用 Ruby 语言在不同的行中显示每个对象的属性以提高命令行的可读性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35598580/

相关文章:

ruby - 将 stderr 重定向到 Logger 实例

ruby-on-rails - ruby - 将数组写入散列而不覆盖

linux - 没有清除的定时器显示脚本

r - 使用 root 安装的 R 包的问题

c# - 如何从代码运行命令行命令

python - 更改 Python Cmd 模块处理自动完成的方式

Ruby tcpserver 客户端服务器

ruby - 使用 block 配置类

linux - 如何将 Windows Identity 从 Angular 应用程序传递到托管在 Linux 服务器上的 asp.net core 2.0 webapi

c - 如何使用 C 语言访问命令行应用程序包