YAML for Ruby: How to turn off the boolean interpretations?

Discussion in 'Ruby' started by mikshir, Dec 9, 2005.

  1. mikshir

    mikshir Guest

    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.
     
    mikshir, Dec 9, 2005
    #1
    1. Advertising

  2. mikshir

    daz Guest

    mikshir wrote:
    >
    > Problem: Ruby wants to auto-interpret boolean flagged words, [...]
    >


    Hi Mikshir,

    Hadn't heard about those :-? Interesting.

    >
    > 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.
    >


    This isn't anything official, so caution is recommended.

    Successive changes to YAML.rb mean that I've had to stitch
    two fixes. The relevant one should auto-select and it
    seems to work for the following Rubys:

    1.8.0 (2003-08-30)
    1.8.2 (2004-12-25)
    1.8.3 (2005-07-22)
    1.8.4 (2005-12-01)
    1.9.0 (2005-10-31)

    It re-categorises all bool types as normal strings and
    doesn't touch any other types.

    Note that, as well as inserting the module addition in your
    scripts, you must use:
    YAML.parse(foo).transform
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    in place of YAML.load
    http://yaml4r.sourceforge.net/doc/class/yaml_parser_parse_method.htm


    <fix/test below> - email me if any problem(s).


    daz
    --

    require 'yaml'

    #----------------------------------------------------------------------
    module YAML::Syck
    BOOL_RE = /bool#\w+\z/
    if defined?(Loader)
    class Loader
    alias_method :eek:_transfer, :transfer
    def transfer(type_id, val)
    (String === type_id) and type_id.gsub!(BOOL_RE, 'str')
    o_transfer(type_id, val)
    end
    end
    else
    class Node
    alias_method :eek:_transform, :transform
    def transform
    kind == :scalar and self.type_id = type_id.gsub(BOOL_RE, 'str')
    o_transform
    end
    end
    end
    end
    #----------------------------------------------------------------------

    hobj = YAML.parse(<<EoS).transform
    ---
    words:
    - yes
    - put
    - it
    - on
    - the
    - off
    - setting
    EoS


    if hobj['words'] == %w{ yes put it on the off setting }
    puts ' ***---> S U C C E S S <---***'
    else
    p hobj
    end
     
    daz, Dec 10, 2005
    #2
    1. Advertising

  3. 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.
     
    Logan Capaldo, Dec 10, 2005
    #3
  4. mikshir

    daz Guest

  5. mikshir

    Guest

    On Sat, 10 Dec 2005, 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.


    why not simply conform to the spec? :

    harp:~ > cat a.rb
    doc = <<-yaml
    ---
    "words":
    - "yes"
    - "put"
    - "it"
    - "on"
    - "the"
    - "off"
    - "setting"
    yaml

    doc = STDIN.read unless STDIN.tty?

    require "yaml" and y(YAML::load(doc))


    harp:~ > ruby a.rb
    words:
    - "yes"
    - put
    - it
    - "on"
    - the
    - "off"
    - setting


    harp:~ > ruby a.rb|ruby a.rb
    words:
    - "yes"
    - put
    - it
    - "on"
    - the
    - "off"
    - setting


    as you can see these words need to be quoted to be handled correctly -
    basically the error is both in the person(s) writing the docs and in the other
    decoders. i'll bet a quoted "yes" or "no" works in perl and python decoders
    too though.

    regards.

    -a
    --
    ===============================================================================
    | ara [dot] t [dot] howard [at] noaa [dot] gov
    | all happiness comes from the desire for others to be happy. all misery
    | comes from the desire for oneself to be happy.
    | -- bodhicaryavatara
    ===============================================================================
     
    , Dec 10, 2005
    #5
  6. On Dec 10, 2005, at 2:16 AM, wrote:
    >
    > why not simply conform to the spec? :
    >
    >


    I believe that the documents are being generated by Python and/or
    Perl and he is (for whatever reason) not allowed to mess with that part.
     
    Logan Capaldo, Dec 10, 2005
    #6
  7. mikshir

    mikshir Guest

    Thanks for the followups, gang.

    Yes, I have my own modified version of PyYaml to make it conform to the
    latest Ruby implemention. (I haven't done this for the Perl YAML
    module yet.) The immediate issue is that for certain reasons I can't
    update the modules system-wide and so I was looking for a quick and
    easy user-script solution; like throwing in some re-definitions,
    overrides, or extensions to force in some backwards compatability and
    something I can throw in a library or with a switch.

    Quoting the flagged words does the right thing (forces them to be
    strings) and pre/post processing all Python and Perl generated Yaml
    docs has been a stop-gap measure but is clumsy or not convenient.

    Anyhow, thanks very much Daz. I will test out your method. /salute
     
    mikshir, Dec 16, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. robert112
    Replies:
    1
    Views:
    476
    Juan T. Llibre
    Apr 26, 2007
  2. J Leonard
    Replies:
    4
    Views:
    12,891
    Mark Space
    Jan 19, 2008
  3. brian.mills

    Overloading SOAP Interpretations for Specific Primitive Types

    brian.mills, Nov 17, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    194
    Dan Rogers
    Nov 18, 2004
  4. Eric Promislow
    Replies:
    4
    Views:
    235
    Eric Promislow
    Oct 31, 2006
  5. Jim Burgess

    Turn off warnings in ruby

    Jim Burgess, Jan 8, 2009, in forum: Ruby
    Replies:
    6
    Views:
    139
    Jim Burgess
    Jan 10, 2009
Loading...

Share This Page