views:

607

answers:

3

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
=> "--- \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"

Thoughts?

+1  A: 

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

sepp2k
+6  A: 

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

y ProductColor.all

Assuming you are in script/console

ryanb
+8  A: 

You should try hirb. It's a gem made to to pretty format objects in the ruby console. Your script/console session would look like this:

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

You can learn more about hirb at its homepage.

ryanb's answer is basically what I was looking for, but this is too cool not to accept.
Horace Loeb
While not an answer to the original question, it might be pointing out that you can add the hirb stuff to your ~/.irbrc so you don't have to require and enable it each time.
jordelver