Rails 콘솔에서 멋진 포맷을 얻는 방법
다음과 같이 멋지게 보이기를 원합니다.
>> 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 형식으로 출력
>> puts ProductColor.all.to_yaml
간단하게 잘 작동합니다!
출처 : https://stackoverflow.com/a/4830096
안녕하세요, 스크립트 / 콘솔에서 시도해 볼 수도 있습니다.
>> y ProductColor.all
당신을 위해 작동하지 않습니다.
이 시도:
>> require 'yaml'
>> YAML::ENGINE.yamler = 'syck'
그때
>> y ProductColor.all
나는 그것을 작동시키는 데 문제가 있었으므로 awesome_print에 2 센트를 추가 할 것입니다 .Gemfile에 이것을 추가하십시오. :development
gem 'awesome_print', require: 'ap'
그런 다음
rails console
넌 할 수있어
> ap Model.all
그게 다야. 그러나 당신은 또한 추가 할 수 있습니다
require "awesome_print"
AwesomePrint.irb!
~ / .irbrc에 콘솔을 열 때마다 awesome_print가 필요하며 간단하게 할 수 있습니다.
Model.all without the need of typing ap
Use irbtools
gem.
It will automatically format the the console output plus you'll get tons of great features.
You might want to define ProductColor's inspect method to return something that you find nice. For example:
def inspect
"<#{id} - #{name} (#{internal_name})>"
end
After which the result of ProductColor.all will display as something like [<1 - White (White)>, ...]. Of course you should adjust the inspect method to your needs, so that it displays all the information you need in a style that you like.
Edit: also if the issue was the lack of line breaks in the output, you might try
require 'pp'
pp ProductColor.all
which should insert linebreaks where appropriate
To add to Alter Lago's suggestion for using AwesomePrint, If you can't/shouldn't/don't want to add the awesome_print gem to your project's Gemfile, do this:
gem install awesome_print
Edit ~/.irb.rc and add this:
$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'
(Making sure the path and version are correct, of course)
You may also try the following for a group of objects
Object.all.map(&:attributes).to_yaml
This will give you much nicer output, like
---
id: 1
type: College
name: University of Texas
---
id: 2
type: College
name: University of California
Calling to_yaml
on attributes rather than the object itself saves you from viewing the full contents of the object in the output
Or puts Object.last.attributes.to_yaml
for a single object
Shorthand is also available: y Object.last.attributes
I think this solution is the most accurate one. You should try this:
puts JSON.pretty_generate Entry.all.map(&:attributes)
This will give you a super nice output compare to YAML format:
[
{
"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"
}
]
참고URL : https://stackoverflow.com/questions/1224525/how-to-get-nice-formatting-in-the-rails-console
'programing tip' 카테고리의 다른 글
Gradle 빌드 유형별로 앱 이름을 변경하는 방법 (0) | 2020.07.13 |
---|---|
AppCompat 라이브러리에서 SwitchCompat의 색상을 변경하는 방법 (0) | 2020.07.13 |
PHP 헤더가있는 CORS (0) | 2020.07.13 |
UIImageView에서 코너 반경 설정이 작동하지 않습니다 (0) | 2020.07.13 |
groovy 배열 / 해시 / 컬렉션 / 목록의 요소를 확인하는 방법은 무엇입니까? (0) | 2020.07.13 |