如何使用Ruby语言在命令行中以不同的行显示每个对象的属性以便于阅读?

时间:2016-02-24 09:48:14

标签: 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)> 

2 个答案:

答案 0 :(得分:0)

您可以使用Ruby中包含的PrettyPrint标准库。

[Reasons]
'Customer is not interested'
'Customer bought elsewhere'
'Customer is busy'

[Comments]
'Spoke to mr Smith who asked to call laterCustomer is busy'
'hung the phone on meCustomer is not interested'

或者如果你想要更多的控制,比如颜色,缩进等,你可以尝试awesome print gem。

使用require 'pp' h = {mula: 'khao', alu: 'khamu na', lorem: 'You can use the PrettyPrint standard library included in Ruby.'} pp h #=> {:mula=>"khao", # :alu=>"khamu na", # :lorem=>"You can use the PrettyPrint standard library included in Ruby."} 进行更新:

Struct

答案 1 :(得分:0)

如果您使用的是Rails漂亮的打印对象attributes

pp job.subscription.attributes

或类似地:

pp job.attributes

否则你可以将对象转换为Hash并使用pp进行打印。