Girders Blog
Notes on building internet applications

Ruby YAML loads leading-zero numbers as Octal

Aug 13, 2009

Watch out! I was innocently loading a YAML file in Ruby created by another system

value: 012

The Ruby YAML library interprets the 012 as octal, as it would in Ruby source code (and as Perl would do). To have this interpreted as string, you must quote the value.

value: “012”

In case you didn’t know, any number starting with a leading zero in C, Ruby, Perl, or Python, is assumed to be octal. “Of course!”, you say, “Everyone thinks and codes in octal”. The only time I use octal is using the unix permissions on the “chmod” command—but now there are modern alternatives to that.

Hexadecimal numbers start with “0x” such as “0x12”. That is not a hard mistake to find as its not a valid integer. But innocent leading-zero numbers can cause syntax errors if the number contains a 9 (non-octal digit) such as “09”, or even floating point numbers  like “012.345”.

You can test this in “irb”…

$ irb
» 07
=> 7
» 012
=> 10
» 0x12
=> 18
» 019
SyntaxError: compile error…
» 012.345
SyntaxError: compile error…

So this is error-prone and annoying in these languages.

As it turns out, this behavior is part of the YAML specification, but I do not like it!  I doubt many people expect that behavior. Data encoding should not take these “short cuts”, as YAML files can be created by non-programmers who have never heard of octal and expect a leading-zero to be harmless.

I’m finding the rule of thumb in YAML is to use quoted values when possible.