Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Dweomer in YAML::Syck and Ruby

by diotalevi (Canon)
on Jan 24, 2009 at 11:15 UTC ( #738671=perlmeditation: print w/replies, xml ) Need Help??

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 YAML 1.1+’s type system is still expansive but it is also still linked from the yaml spec at

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


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

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://738671]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2020-10-30 19:16 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (283 votes). Check out past polls.