質問
用していただきたいと思いこんな感じですね:
>> 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'
から ruby-docs
高齢Rubyのバージョンが打ち出されている。-<=1.9では、Syckも提供していると考えられ 完全に除去のリリースのRuby2.0.0.
のためのレールを4/ruby2を使用できるだけ
puts object.to_yaml
他のヒント
hirb を試してください。これは、Rubyコンソールでオブジェクトをきれいにフォーマットするために作られた逸品です。スクリプト/コンソールセッションは次のようになります。
>> 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 / rails / pryコンソールと統合するには、~/.irbrc
または~/.pryrc
ファイルに追加します:
require "awesome_print"
AwesomePrint.irb! # just in .irbrc
AwesomePrint.pry! # just in .pryrc
使用できることにも注意してください:
j ProductColor.all.inspect
YamlではなくJson形式で出力する
こんにちは、スクリプト/コンソールでこれを試すこともできます
>> y ProductColor.all
あなたのために働いていません。
これを試してください:
>> require 'yaml'
>> YAML::ENGINE.yamler = 'syck'
then
<*>機能させるのに苦労したので、2セントをawesome_printに追加します
これをGemfileに、できれば:development
gem 'awesome_print', require: 'ap'
then in
rails console
できること
> ap Model.all
それでおしまい。ただし、追加することもできます
require "awesome_print"
AwesomePrint.irb!
〜/ .irbrcには、この方法でコンソールを開くたびにawesome_printが必要になり、簡単に実行できます
Model.all apと入力する必要なし
irbtools
gemを使用します。
コンソール出力が自動的にフォーマットされ、多くの優れた機能が得られます。
ProductColorのinspectメソッドを定義して、見栄えのよいものを返すことができます。例:
def inspect
"<#{id} - #{name} (#{internal_name})>"
end
その後、ProductColor.allの結果は[<!> lt; 1-白(白)<!> gt ;, ...]のように表示されます。もちろん、必要に応じて必要なすべての情報が好きなスタイルで表示されるように、検査メソッドをニーズに合わせて調整する必要があります。
編集:出力に改行がないことも問題だった場合、試してみてください
require 'pp'
pp ProductColor.all
必要に応じて改行を挿入する
AwesomePrintの使用に関するAlter Lagoの提案に追加するには、 プロジェクトのGemfileにawesome_print gemを追加できない/できない/したくない場合は、次のようにします。
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"
}
]