YAML has a thing called “implicit typing” where unquoted values are translated into language-specific entities. What you expect is what when you write the number 1, you might get an integer back. You do. What you might not expect is that when you type the word yes, you get a boolean back and not the word.

We are using YAML 1.0 but the documentation for this has fallen off the internet. It can still be retrieved from the internet archive at http://web.archive.org/web/20041204101603/http://yaml.org/spec/type.html. YAML 1.1+’s type system is still expansive but it is also still linked from the yaml spec at http://yaml.org/type/.

The following YAML document does all kinds of strange things depending on whether Ruby or Perl is reading and writing it. In general, Ruby’s default library is more magical and requires hand-holding to cope with whatever it did. To force “ordinary” interpretations, quote everything. Be liberal with it. Quote more than you think a person should ever reasonably be expected to quote.

The following YAML document is 99% magic. The only non-magical things are the strings bool, float, int, and date. Everything else is potentially transmogrified. Everything else is potentially transformed by YAML’s implicit typing.

--- bool: true: [ y, Y, yes, Yes, YES, true, True, TRUE, on, On, ON ] false: [ n, N, no, No, NO, false, False, FALSE, off, Off, OFF ] float: [ .inf, +.inf, -.inf, .nan ] int: [ 0b10, 010, 0xf, 19:59 ] null: [ ~, null ] date: [ 2008-10, 2008-10-02T21:21:42Z-08 ]

The canonical representation for booleans in yaml are the unquoted literals y and n but yaml.rb treats them like strings while converting everything else. Perl’s YAML::Syck defaults to treating things purely as strings unless it is asked to apply implicit typing and then it acts like Ruby. Notice the odd base-60 integer conversion from 19:59 -> 1199. Notice that all integer conversions are valid except binary which as inexplicably left off.

Ruby’s yaml.rb

y -> 'y' Y -> 'Y' Yes, Yes, YES, true, True, TRUE, on, On, ON -> true n -> 'n' N -> 'N' no, No, NO, false, False, FALSE, off, Off, OFF -> false 19:59 -> 1199

Perl’s YAML::Syck, $YAML::Syck::ImplicitTyping = 1

y -> 'y' Y -> 'Y' Yes, Yes, YES, true, True, TRUE, on, On, ON -> '1' / 1 n -> 'n' N -> 'N' no, No, NO, false, False, FALSE, off, Off, OFF -> '' / 0

Ruby's yaml.rb

.inf, +.inf -> Infinity -.inf -> -Infinity .nan -> NaN ~, null -> nil

Perl

.inf, +.inf -> 'inf' / inf -.inf -> '-inf' / -inf .nan -> 'nan' / nan ~, null -> undef

I separated inf, nan, and nil/undef out because their target language implementations are different while the meaning is identical. If you treat the perl strings ‘inf’, ‘-inf’, ‘+inf’, ‘nan’ like numbers, they are numified as the equivalent floating point value. Also, Ruby’s nil and Perl’s undef are really the same thing but they go by different names.

Thought I’d share. Remember, quote, quote, quote, when in doubt, quote. When in Ruby, always, always convert with to_i, to_s or whatever is appropriate for the data. There are some other issues related to undesired de-quoting by passing data through yaml.rb that make the actual type of a value unpredictable.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊


In reply to Dweomer in YAML::Syck and Ruby by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.