Rails 콘솔에서 멋진 형식을 지정하는 방법
-
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가 언급 했듯이이 답변은 구식입니다. Rails 3.2+의 경우 다음 코드를 실행해야합니다. y
작업 방법 :
YAML::ENGINE.yamler = 'syck'
에서 루비 닥스
오래된 루비 버전에서는, 즉. <= 1.9, Syck는 여전히 제공되지만 Ruby 2.0.0의 출시로 완전히 제거되었습니다.
Rails 4/Ruby 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/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'
그 다음에
>> y ProductColor.all
나는 그것을 작동시키는 데 약간의 문제가 있었으므로 sauding_print에 2 센트를 추가 할 것입니다. :development
gem 'awesome_print', require: 'ap'
그런 다음
rails console
넌 할 수있어
> ap Model.all
그게 다야. 그러나 추가 할 수도 있습니다
require "awesome_print"
AwesomePrint.irb!
당신의 ~/.irbrc에게,이 방법은 콘솔을 열 때 언제든지 굉장한 _print가 필요하며 간단히 할 수 있습니다.
AP를 입력 할 필요없이 모든 모델
사용 irbtools
보석.
콘솔 출력을 자동으로 포맷하고 수많은 훌륭한 기능을 얻을 수 있습니다.
ProductColor의 검사 방법을 정의하여 좋은 것을 반환 할 수 있습니다. 예를 들어:
def inspect
"<#{id} - #{name} (#{internal_name})>"
end
그 후 ProductColor.all의 결과는 [<1 -White (White)>, ...]와 같은 것으로 표시됩니다. 물론 검사 방법을 필요에 맞게 조정하여 원하는 스타일로 필요한 모든 정보를 표시하도록해야합니다.
편집 : 또한 문제가 출력에 라인이 부족한 경우 시도 할 수 있습니다.
require 'pp'
pp ProductColor.all
적절한 경우 라인 브레이크를 삽입해야합니다
AwesomePrint를 사용하기위한 Lago의 제안을 추가하려면 Awesome_print 보석을 프로젝트의 보석에 추가하고 싶지 않다면 다음을 수행하십시오.
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"
}
]