Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get YAML in Ruby as of 1.9.3 to dump ASCII-8Bit strings as strings?

Here's the problem: I might have strings that are UTF-8, and I might have strings that are US-ASCII. Regardless of the encoding, I'd like YAML.dump(str) to actually dump String objects, instead of these useless !binary objects as the example shows.

Is there a flag or something I'm not seeing to force YAML.dump() to do the right thing?

Ruby 1.9.1 example

YAML::VERSION              # "0.60"
a = "foo"                  # => "foo"
a.force_encoding("BINARY") # => "foo"
YAML.dump(a)               # => "--- foo\n" 

Ruby 1.9.3 example

YAML::VERSION              # "1.2.2"
a = "foo"                  #  => "foo" 
a.force_encoding("BINARY") #  => "foo" 
YAML.dump(a)               # => "--- !binary |-\n  Zm9v\n"

Update: Got my own answer

YAML::ENGINE.yamler='syck'
YAML.dump(a)               # => "--- foo\n" 

So, looks like using the old yamler engine with force the old behavior.

like image 768
Dead Pixel Avatar asked Apr 15 '12 21:04

Dead Pixel


1 Answers

Update: Got my own answer

YAML::ENGINE.yamler='syck'
YAML.dump(a)               # => "--- foo\n" 
like image 108
Dead Pixel Avatar answered Oct 24 '22 01:10

Dead Pixel