On Dec 9, 2005, at 3:57 PM, mikshir wrote:

> I have YAML documents generated in Perl and Python that I process with
> Ruby.
>
> Problem: Ruby wants to auto-interpret boolean flagged words, example:
>
> ---
> words:
>   - yes
>   - put
>   - it
>   - on
>   - the
>   - off
>   - setting
>
> gets loaded in Ruby as
>
> [true, "put", "it", true, "the", false, "setting"]
>
> Basically the latest Perl and Python YAML modules I could find won't
> quote some of the words that Ruby's YAML would. (Things like on, off,
> yes, no, +) Perhaps they conform to a different version of the YAML
> spec. I find the incompatibility a bit frustrating.  I can and have
> easily hacked the other modules to do the compatible thing but there
> are political admin maintenance issues surrounding my patching all the
> company computers with this.
>
> I'll not comment right now on whether I think it was a good idea to
> flag these items as special to begin with.  I'd just like to know if
> there's a simple module over-ride hack or module parameter setting  
> that
> I can set or clip into my scripts to deal with this problem.
>
> Thanks.
>
>

Well, I was looking at the YAML site, and apparently yes/no on/off  
are part of a draft specification, it looks like ruby is just ahead  
of the curve, so to speak.

http://yaml.org/type/bool.html

This of course, fails to help you.

The only solution I could come up with, in a word, sucks.


require 'yaml'

test = YAML.parse(FIle.read("test.yml")) # where test.yml contains  
that ---\n words: -yes -no etc.
test.children[0].value.each do |x|
       if x.type_id =~ /bool#(?:yes|no)\z/
             x.type_id = "tag:yaml.org,2002:str"
       end
end

results = test.transform #=> {"words"=>["yes", "put", "it", "on",  
"the", "off", "setting"]}


Of course this iteration will only work for that file, since I made  
some massive assumptions about the structure. Your other option is to  
do a search and replace on the yess, ons,offs, etc and quote them  
before parsing.