How to get nice formatting in the Rails console

前端 未结 12 1456
一生所求
一生所求 2020-12-12 10:50

I want to get something like this to look nice:

>> ProductColor.all
=> [#

        
12条回答
  •  暖寄归人
    2020-12-12 11:21

    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
    

提交回复
热议问题