Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

irb - How to get nice formatting in the Rails console

I want to get something like this to look nice:

>> 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">]

This doesn't work:

>> 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">]"

And neither does this:

>> ProductColor.all.to_yaml
=> "--- 
- !ruby/object:ProductColor 
  attributes: 
    name: White
    created_at: 2009-06-10 04:02:44
    updated_at: 2009-06-10 04:02:44
    id: "1"
    internal_name: White
  attributes_cache: {}

- !ruby/object:ProductColor 
  attributes: 
    name: Ivory
    created_at: 2009-06-10 04:02:44
    updated_at: 2009-06-10 04:02:44
    id: "2"
    internal_name: Ivory
  attributes_cache: {}

- !ruby/object:ProductColor 
  attributes: 
    name: Blue
    created_at: 2009-06-10 04:02:44
    updated_at: 2009-06-10 04:02:44
    id: "3"
    internal_name: Light Blue
  attributes_cache: {}

- !ruby/object:ProductColor 
  attributes: 
    name: Green
    created_at: 2009-06-10 04:02:44
    updated_at: 2009-06-10 04:02:44
    id: "4"
    internal_name: Green
  attributes_cache: {}

"

Thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The y method is a handy way to get some pretty YAML output.

y ProductColor.all

Assuming you are in script/console

As jordanpg commented, this answer is outdated. For Rails 3.2+ you need to execute the following code before you can get the y method to work:

YAML::ENGINE.yamler = 'syck'

From ruby-docs

In older Ruby versions, ie. <= 1.9, Syck is still provided, however it was completely removed with the release of Ruby 2.0.0.

For rails 4/ruby 2 you could use just

puts object.to_yaml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...