Issue #5923 has been reported by Nathan Witmer.

----------------------------------------
Bug #5923: YAML serialization of binary string broken with psych engine
https://bugs.ruby-lang.org/issues/5923

Author: Nathan Witmer
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0] 


Using the psych engine in 1.9.3, some binary strings cannot be serialized. Instead, they result in a runtime error:

    /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/emitter.rb:24:in `scalar': expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS (RuntimeError)

To reproduce:

$ cat a.rb
# encoding: UTF-8

require "yaml"

binary = "\xC37\xA2\xC0=[\"\xE7\x06\x19\xAA1\x95\x12\xA1z"
binary.force_encoding "ascii-8bit" if binary.respond_to? :force_encoding

puts "\n*** ruby #{RUBY_VERSION}"

# test for ruby 1.9, both yaml engines:
if YAML.const_defined?(:ENGINE)
  
  puts "syck engine:"
  YAML::ENGINE.yamler = "syck"
  puts binary.to_yaml

  puts "psych engine:"
  YAML::ENGINE.yamler = "psych"
  puts binary.to_yaml

else

  # otherwise, just dump it:
  puts binary.to_yaml

end

$ rvm 1.9.3-p0,1.8.7-p352 do ruby a.rb

*** ruby 1.9.3
syck engine:
--- "\xC37\xA2\xC0=[\"\xE7\x06\x19\xAA1\x95\x12\xA1z"
psych engine:
/Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/emitter.rb:24:in `scalar': expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS (RuntimeError)
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/emitter.rb:24:in `visit_Psych_Nodes_Scalar'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/visitor.rb:15:in `visit'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/visitor.rb:5:in `accept'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/emitter.rb:19:in `block in visit_Psych_Nodes_Document'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/emitter.rb:19:in `each'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/emitter.rb:19:in `visit_Psych_Nodes_Document'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/visitor.rb:15:in `visit'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/visitor.rb:5:in `accept'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/emitter.rb:13:in `block in visit_Psych_Nodes_Stream'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/emitter.rb:13:in `each'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/emitter.rb:13:in `visit_Psych_Nodes_Stream'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/visitor.rb:15:in `visit'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/visitors/visitor.rb:5:in `accept'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/nodes/node.rb:46:in `to_yaml'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych.rb:190:in `dump'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/psych/core_ext.rb:14:in `psych_to_yaml'
	from /Users/nathan/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck/rubytypes.rb:170:in `to_yaml'
	from a.rb:19:in `<main>'

*** ruby 1.8.7
--- !binary |
wzeiwD1bIucGGaoxlRKheg==



-- 
http://bugs.ruby-lang.org/