Using Psych::Coder to emit a hash behaves differently than emitting a hash directly #107

Closed
viking opened this Issue Dec 11, 2012 · 2 comments

Comments

3 participants

viking commented Dec 11, 2012

Consider this code:

require 'yaml'

class Foo
  attr_reader :hash

  def initialize(hash)
    @hash = hash
  end

  def encode_with(coder)
    coder.represent_map(nil, @hash)
  end
end

foo_with_string_keys = Foo.new("foo" => "bar")
foo_with_string_keys.hash.to_yaml #=> "---\nfoo: bar\n"
foo_with_string_keys.to_yaml      #=> "---\nfoo: bar\n"

foo_with_sym_keys = Foo.new(:foo => "bar")
foo_with_sym_keys.hash.to_yaml    #=> "---\n:foo: bar\n"
foo_with_sym_keys.to_yaml         #=> TypeError: wrong argument type Symbol (expected String)

The problem is here: https://github.com/tenderlove/psych/blob/master/lib/psych/visitors/yaml_tree.rb#L440

In the emit_coder function, map emitting doesn't behave the same way as in visit_Hash. In particular, it fails when there is a symbol for a key.

Is this intended?

Thanks in advance!

Owner

tenderlove commented Dec 11, 2012

Nope, this is a bug. I will fix it. Thanks!

@tjwallace tjwallace added a commit to tjwallace/psych that referenced this issue Jan 25, 2013

@tjwallace tjwallace Fix case when map includes a symbol key in #emit_coder
Mimic what is done in #visit_Hash. Fixes #107.
43a15d3
Contributor

tjwallace commented Jan 31, 2013

I've made an attempt to fix the issue in #122, let know what you think!

tenderlove closed this in d289285 Apr 5, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment