From f4519d56ebd4ca129669e212f46eab782e642483 Mon Sep 17 00:00:00 2001 From: bradleyjucsc Date: Thu, 23 Sep 2010 12:12:24 -0700 Subject: Changed sort to sort_by in Plist::Emit.plist_node to allow mixed symbol and string hash keys. --- lib/plist/generator.rb | 9 +-------- test/test_generator.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/plist/generator.rb b/lib/plist/generator.rb index 02958c5..89d4132 100644 --- a/lib/plist/generator.rb +++ b/lib/plist/generator.rb @@ -78,7 +78,7 @@ module Plist::Emit else inner_tags = [] - element.keys.sort.each do |k| + element.keys.sort_by{|k| k.to_s }.each do |k| v = element[k] inner_tags << tag('key', CGI::escapeHTML(k.to_s)) inner_tags << plist_node(v) @@ -213,13 +213,6 @@ module Plist::Emit end end -# we need to add this so sorting hash keys works properly -class Symbol #:nodoc: - def <=> (other) - self.to_s <=> other.to_s - end -end - class Array #:nodoc: include Plist::Emit end diff --git a/test/test_generator.rb b/test/test_generator.rb index dbb1a59..b03001c 100644 --- a/test/test_generator.rb +++ b/test/test_generator.rb @@ -51,4 +51,25 @@ class TestGenerator < Test::Unit::TestCase File.unlink('test.plist') end + + # The hash in this test was failing with 'hsh.keys.sort', + # we are making sure it works with 'hsh.keys.sort_by'. + def test_sorting_keys + hsh = {:key1 => 1, :key4 => 4, 'key2' => 2, :key3 => 3} + output = Plist::Emit.plist_node(hsh) + expected = <<-STR + + key1 + 1 + key2 + 2 + key3 + 3 + key4 + 4 + + STR + + assert_equal expected, output.gsub(/[\t]/, "\s\s") + end end -- cgit v1.2.1