Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
XP is just a number
 
PerlMonks  

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

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Comment on Dweomer in YAML::Syck and Ruby
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-04-19 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls