Bug in Ruby YAML interpreter?

Discussion in 'Ruby' started by Ralph Shnelvar, Feb 12, 2010.

  1. I have a YAML file that looks something like this:


    en:
    countries:
    NI: "Nicaragua"
    NL: "Netherlands"
    NO: "Norway"
    NP: "Nepal"
    NR: "Nauru"
    NU: "Niue"




    When I run this through
    ------------------------------------

    require 'yaml'

    countries = []
    countries_str = IO.read("lib\\locale\\en.yml"); nil
    deserialized = YAML::load(countries_str); nil
    puts "deserialized['en']['countries'].to_a.size=" + deserialized['en']['countries'].to_a.size.to_s
    # puts deserialized['en']['countries'].to_a
    deserialized['en']['countries'].each do |country|
    # puts at_file_line_msg(__FILE__, __LINE__)
    # puts country.class
    p country[0]
    p country[1]
    countries << country[0]
    end



    country[0] comes up as false rather than as "NO".

    Is this my weak understanding or is this a bug?
     
    Ralph Shnelvar, Feb 12, 2010
    #1
    1. Advertising

  2. Ralph Shnelvar

    Ryan Davis Guest

    On Feb 12, 2010, at 14:29 , Ralph Shnelvar wrote:

    > I have a YAML file that looks something like this:
    >
    >
    > en:
    > countries:
    > NI: "Nicaragua"
    > NL: "Netherlands"
    > NO: "Norway"
    > NP: "Nepal"
    > NR: "Nauru"
    > NU: "Niue"
    > ...
    > country[0] comes up as false rather than as "NO".
    >
    > Is this my weak understanding or is this a bug?


    that's per spec... quote it to ensure the string NO.
     
    Ryan Davis, Feb 12, 2010
    #2
    1. Advertising

  3. Ralph Shnelvar wrote:
    > I have a YAML file that looks something like this:
    >
    > en:
    > countries:
    > [...]
    > NO: "Norway"
    > [...]
    > country[0] comes up as false rather than as "NO".
    >
    > Is this my weak understanding or is this a bug?


    This is how Booleans are represented in YAML. An unescaped string
    which is either "NO", "YES", "TRUE", "FALSE", "ON", "OFF", "Y" or "N"
    (case-insensitive) is a Boolean value and not a string.

    You need to escape the string:

    "NO": "Norway"

    BTW: If you hadn't put all those '; nil' there, you would have seen
    the 'false => "Norway"' key in the hash printed out in IRB.

    jwm
     
    Jörg W Mittag, Feb 12, 2010
    #3
    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. RubyQuestions
    Replies:
    0
    Views:
    236
    RubyQuestions
    Dec 3, 2003
  2. Paul Battley

    YAML.dump/YAML.load bug

    Paul Battley, Aug 3, 2005, in forum: Ruby
    Replies:
    0
    Views:
    194
    Paul Battley
    Aug 3, 2005
  3. Eric Promislow
    Replies:
    4
    Views:
    222
    Eric Promislow
    Oct 31, 2006
  4. Joshua Choi

    yaml.rb and YAML "%" directives

    Joshua Choi, Jan 14, 2007, in forum: Ruby
    Replies:
    1
    Views:
    184
  5. Fransiscus Xaverius

    YAML Problem YAML::Object

    Fransiscus Xaverius, Dec 14, 2007, in forum: Ruby
    Replies:
    2
    Views:
    163
    Fransiscus Xaverius
    Dec 14, 2007
Loading...

Share This Page