كيفية الحصول على لطيفة التنسيق في وحدة القضبان
-
22-07-2019 - |
سؤال
أريد الحصول على شيء مثل هذا أن تبدو لطيفة:
>> ProductColor.all
=> [#<ProductColor id: 1, name: "White", internal_name: "White", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 2, name: "Ivory", internal_name: "Ivory", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 3, name: "Blue", internal_name: "Light Blue", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">, #<ProductColor id: 4, name: "Green", internal_name: "Green", created_at: "2009-06-10 04:02:44", updated_at: "2009-06-10 04:02:44">]
هذا لا يعمل:
>> ProductColor.all.inspect
=> "[#<ProductColor id: 1, name: \"White\", internal_name: \"White\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 2, name: \"Ivory\", internal_name: \"Ivory\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 3, name: \"Blue\", internal_name: \"Light Blue\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">, #<ProductColor id: 4, name: \"Green\", internal_name: \"Green\", created_at: \"2009-06-10 04:02:44\", updated_at: \"2009-06-10 04:02:44\">]"
ولا هذا:
>> ProductColor.all.to_yaml
=> "--- \n- !ruby/object:ProductColor \n attributes: \n name: White\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"1\"\n internal_name: White\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Ivory\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"2\"\n internal_name: Ivory\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Blue\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"3\"\n internal_name: Light Blue\n attributes_cache: {}\n\n- !ruby/object:ProductColor \n attributes: \n name: Green\n created_at: 2009-06-10 04:02:44\n updated_at: 2009-06-10 04:02:44\n id: \"4\"\n internal_name: Green\n attributes_cache: {}\n\n"
الأفكار ؟
المحلول
وطريقة y
هو طريقة سهلة للحصول على بعض الانتاج YAML جميلة.
y ProductColor.all
وعلى افتراض انك في script/console
وكما jordanpg علق، وهذا الجواب هو عفا عليها الزمن. لالقضبان 3.2+ تحتاج إلى تنفيذ التعليمات البرمجية التالية قبل أن تتمكن من الحصول على طريقة y
للعمل:
YAML::ENGINE.yamler = 'syck'
<اقتباس فقرة>
في الإصدارات القديمة روبي، أي. <= 1.9، Syck لا تزال تقدم، إلا أنه تمت إزالة تماما مع الافراج عن روبي 2.0.0.
اقتباس فقرة>لالقضبان 4 / روبي 2 هل يمكن استخدام فقط
puts object.to_yaml
نصائح أخرى
ويجب عليك أن تحاول hirb . إنها جوهرة المبذولة لكائنات شكل جميلة في وحدة تحكم روبي. ان جلسة النصي / وحدة التحكم الخاصة بك تبدو هذه:
>> require 'hirb'
=> true
>> Hirb.enable
=> true
>> ProductColor.first
+----+-------+---------------+---------------------+---------------------+
| id | name | internal_name | created_at | updated_at |
+----+-------+---------------+---------------------+---------------------+
| 1 | White | White | 2009-06-10 04:02:44 | 2009-06-10 04:02:44 |
+----+-------+---------------+---------------------+---------------------+
1 row in set
=> true
ويمكنك معرفة المزيد حول hirb في دورته الرئيسية .
رائع الطباعة هو لطيف جدا إذا كنت تريد كائن بادئة. شيء من هذا القبيل:
$ rails console
rails> require "awesome_print"
rails> ap Account.all(:limit => 2)
[
[0] #<Account:0x1033220b8> {
:id => 1,
:user_id => 5,
:assigned_to => 7,
:name => "Hayes-DuBuque",
:access => "Public",
:website => "http://www.hayesdubuque.com",
:toll_free_phone => "1-800-932-6571",
:phone => "(111)549-5002",
:fax => "(349)415-2266",
:deleted_at => nil,
:created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
:updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
:email => "info@hayesdubuque.com",
:background_info => nil
},
[1] #<Account:0x103321ff0> {
:id => 2,
:user_id => 4,
:assigned_to => 4,
:name => "Ziemann-Streich",
:access => "Public",
:website => "http://www.ziemannstreich.com",
:toll_free_phone => "1-800-871-0619",
:phone => "(042)056-1534",
:fax => "(106)017-8792",
:deleted_at => nil,
:created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
:updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
:email => "info@ziemannstreich.com",
:background_info => nil
}
]
لدمجه بشكل افتراضي مع وحدة IRB / القضبان / حدق، إضافة إلى ملف ~/.irbrc
أو ~/.pryrc
:
require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc
ويمكن أيضا الإشارة التي يمكنك استخدامها:
j ProductColor.all.inspect
وإلى الإخراج في تنسيق JSON بدلا من YAML
مرحبا يمكنك أيضا محاولة هذا في السيناريو الخاص بك/وحدة التحكم إذا
>> y ProductColor.all
لا يعمل بالنسبة لك.
جرب هذا:
>> require 'yaml'
>> YAML::ENGINE.yamler = 'syck'
ثم
>> y ProductColor.all
وكان لي بعض المتاعب مما يجعل من العمل حتى سأضيف بلدي اثنين سنتا الى awesome_print
هذا إضافة إلى Gemfile الخاص بك، ويفضل في :development
وgem 'awesome_print', require: 'ap'
ثم في
وrails console
ويمكنك القيام به
و> ap Model.all
هذا هو. ولكن يمكنك أيضا إضافة
require "awesome_print"
AwesomePrint.irb!
لديك ~ / .irbrc، سوف تكون هناك حاجة بهذه الطريقة awesome_print في أي وقت قمت بفتح وحدة التحكم ويمكنك ببساطة القيام به
وModel.all من دون الحاجة إلى كتابة ا ف ب
استخدم irbtools
الأحجار الكريمة.
وسيكون على شكل تلقائيا في وحدة الانتاج بالإضافة إلى أنك سوف تحصل على طن من الميزات الرائعة.
وقد ترغب في تحديد الطريقة تفقد ProductColor للعودة شيء أن تجد لطيفة. على سبيل المثال:
def inspect
"<#{id} - #{name} (#{internal_name})>"
end
وبعد ذلك نتيجة ProductColor.all سيتم عرض كشيء مثل [<1 - الأبيض (وايت)>، ...]. بالطبع يجب ضبط تفقد طريقة للاحتياجات الخاصة بك، بحيث يعرض كل ما تحتاجه من معلومات في النمط الذي تريد.
وتحرير: أيضا إذا كانت القضية لعدم وجود فواصل الأسطر في الإخراج، قد حاولت
require 'pp'
pp ProductColor.all
والتي ينبغي إدراج بفواصل عند الاقتضاء
لإضافة لتغيير اقتراح لاغو لاستخدام AwesomePrint، إذا كنت لا يمكن / يجب أن لا / لا ترغب في إضافة جوهرة awesome_print إلى Gemfile المشروع الخاص بك، القيام بذلك:
وgem install awesome_print
وتحرير ~ / .irb.rc وإضافة هذا:
و$LOAD_PATH << '/Users/your-user/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib'
وrequire 'awesome_print'
و(التأكد من المسار ونسخة صحيحة، وبطبيعة الحال)
يمكنك أيضا محاولة التالية لمجموعة من الكائنات
Object.all.map(&:attributes).to_yaml
هذا وسوف تعطيك أجمل بكثير الإخراج ، مثل
---
id: 1
type: College
name: University of Texas
---
id: 2
type: College
name: University of California
الدعوة to_yaml
على سمات بدلا من الكائن نفسه يوفر لك من عرض كامل محتويات الكائن في الإخراج
أو puts Object.last.attributes.to_yaml
لكائن واحد
الاختزال هو متاح أيضا: y Object.last.attributes
وأعتقد أن هذا الحل هو الأكثر دقة واحدة. عليك أن تحاول هذه:
puts JSON.pretty_generate Entry.all.map(&:attributes)
وهذا سوف تعطيك الناتج سوبر لطيف مقارنة شكل YAML:
[
{
"id": 44,
"team_id": null,
"member_id": 1000000,
"match_id": 1,
"created_at": "2019-04-09 15:53:14 +0900",
"updated_at": "2019-04-09 15:53:14 +0900"
},
{
"id": 45,
"team_id": null,
"member_id": 1000001,
"match_id": 1,
"created_at": "2019-04-09 15:53:36 +0900",
"updated_at": "2019-04-09 15:53:36 +0900"
},
{
"id": 46,
"team_id": null,
"member_id": 1000003,
"match_id": 1,
"created_at": "2019-04-09 15:56:40 +0900",
"updated_at": "2019-04-09 15:56:40 +0900"
},
{
"id": 47,
"team_id": null,
"member_id": 1000004,
"match_id": 1,
"created_at": "2019-04-09 15:56:48 +0900",
"updated_at": "2019-04-09 15:56:48 +0900"
}
]