From e74f63648098f2784658e67bbd411fc7910faeec Mon Sep 17 00:00:00 2001 From: Nobuhiro IMAI Date: Sun, 11 Oct 2009 12:11:55 +0900 Subject: [PATCH] improve the pretty printing of OrderedHash * new method: ActiveSupport::OrderedHash#pretty_print * add a test for above --- activesupport/lib/active_support/ordered_hash.rb | 14 ++++++++++++++ activesupport/test/ordered_hash_test.rb | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index 4324e40..19559bb 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -124,6 +124,20 @@ module ActiveSupport "#" end + def pretty_print(q) + q.group(0, "#") { + q.breakable " " + q.text "{" + q.group(1) { + q.seplist(self) {|pair| + q.pp pair.first + q.text "=>" + q.pp pair.last + } + } + } + end + private def sync_keys! diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index 15bd571..de5d86d 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -163,6 +163,20 @@ class OrderedHashTest < Test::Unit::TestCase assert @ordered_hash.inspect.include?(@hash.inspect) end + def test_pretty_print + assert_equal "#\n", PP.pp(ActiveSupport::OrderedHash.new, "") + expected = <<'EXPECTED' +#"000099", + "green"=>"009900", + "red"=>"aa0000", + "pink"=>"cc0066", + "orange"=>"cc6633"}> +EXPECTED + PP.pp(@ordered_hash, actual = "") + assert_equal expected, actual + end + def test_alternate_initialization_with_splat alternate = ActiveSupport::OrderedHash[1,2,3,4] assert_kind_of ActiveSupport::OrderedHash, alternate -- 1.6.4.3