Home > Blockchain >  When does ruby parse int in yaml
When does ruby parse int in yaml

Time:12-16

I created this example yaml:

---
01:
  01:
    "01"
  02:
    "02"
  03:
    "03"
  04:
    "04"
  05:
    "05"
  06:
    "06"
  07:
    "07"
  08:
    "08"
  09:
    "09"
  10:
    "10"
  11:
    "11"

When I read this file like the following

require 'yaml'

yml = YAML.load_file(File.join('/home/user', 'test.yml'))
p yml

the output is

{1=>{1=>"01", 2=>"02", 3=>"03", 4=>"04", 5=>"05", 6=>"06", 7=>"07", "08"=>"08", "09"=>"09", 10=>"10", 11=>"11"}}

Now my question is: Why are the keys 1 until 7 parsed as int while the keys 08 and 09 are not parsed as int by rubys yaml-parser but as string, i.e. "08" and "09" respectively?

I am using ruby 3.0.2

CodePudding user response:

The keys 00 to 07 are parsed as octal digits because of the leading 0. Examples:

00
#=> 0
05 
#=> 5
07 
#=> 7

08 and 09 are simply invalid octal digits, and therefore the parser returns those keys in their string representation. Example:

08
#=> Invalid octal digit (SyntaxError)
09
#=> Invalid octal digit (SyntaxError)
  • Related