Hash with both symbol and string keys fails on "sort"
bradleyjucsc opened this issue · comments
{:key1 => 1, :key4 => 4, 'key2' => 2, :key3 => 3}.to_plist
ArgumentError: comparison of String with :key1 failed
from .../.rvm/gems/ruby-1.8.7-p249/gems/plist-3.1.0/lib/plist/generator.rb:81:in sort' from .../.rvm/gems/ruby-1.8.7-p249/gems/plist-3.1.0/lib/plist/generator.rb:81:in
plist_node'
from .../.rvm/gems/ruby-1.8.7-p249/gems/plist-3.1.0/lib/plist/generator.rb:45:in dump' from .../.rvm/gems/ruby-1.8.7-p249/gems/plist-3.1.0/lib/plist/generator.rb:28:in
to_plist'
My solution to this was to just remove the 'sort' totally. You can also get rid of the monkey-patched Symbol method as well if you do that.
If I recall correctly, that's there because certain apps expect dictionaries to have their keys sorted. A better solution would be to change the #sort to #sort_by and call #to_s in the block.
That's unfortunate that the sorting is expected, but given that, I like your solution. Just changing the ".sort" to ".sort_by(&:to_s)" should do the trick then.
ruby-1.8.7-p249 > [:key2,:key1].sort_by(&:to_s)
=> [:key1, :key2]
It is extremely unfortunate :) The biggest culprit is iTunes, so it's not like it can just be ignored.
I'd love a pull request if you feel up to it... one note is that we can't assume that Symbol#to_proc is available, so you'll need to use a real block.
I'll figure out how to do a pull request, and get that to you asap. Interesting bit of info about Symbol#to_proc too, thanks for that.
Moved to a PR in #2