# Bug in YAML for Ruby?

**URL:** <https://www.ruby-forum.com/t/bug-in-yaml-for-ruby/55695>\
**Category:** Ruby\
**Created:** [March 23, 2006, 1:57am UTC](https://www.ruby-forum.com/t/bug-in-yaml-for-ruby/55695 "2006-03-23T01:57:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 23, 2006, 1:57am UTC](https://www.ruby-forum.com/t/bug-in-yaml-for-ruby/55695/1 "2006-03-23T01:57:27Z")

</div>

I’m getting some strange behavior with YAML in ruby regarding the  
characters ‘08’ in a hash.

This code:

# 

require ‘yaml’  
test = { “08” =\> ‘01’, ‘01’ =\> ‘08’, ‘8’ =\> ‘01’  
}  
File.open(“test.yml”, “w”) {|f| YAML.dump(test, f)}

# 

## produces this file:

“01”: 08  
“8”: “01”  
08: “01”

I do not understand why the 08s are not quoted. I tried it under ruby  
1.8.2 and 1.8.4.

Is this expected?

thanks,  
-Matt

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 23, 2006, 2:44am UTC](https://www.ruby-forum.com/t/bug-in-yaml-for-ruby/55695/2 "2006-03-23T02:44:15Z")

</div>

On Mar 23, 2006, at 1:57 AM, Matt R. wrote:

> ## produces this file:
> 
> “01”: 08  
> “8”: “01”  
> 08: “01”
> 
> I do not understand why the 08s are not quoted. I tried it under ruby  
> 1.8.2 and 1.8.4.
> 
> Is this expected?

It is not a bug, but it is confusing. According the the YAML spec[1]  
numbers prefixed with a 0 signal an octal base (as does in Ruby).  
However 08 is not a valid octal number, so it doesn’t get quoted.

[1] [Integer Language-Independent Type for YAML&nbsp;Version&nbsp;1.1](http://yaml.org/type/int.html)

– Daniel

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 23, 2006, 2:47am UTC](https://www.ruby-forum.com/t/bug-in-yaml-for-ruby/55695/3 "2006-03-23T02:47:51Z")

</div>

Matt R. wrote:

> ## produces this file:
> 
> “01”: 08  
> “8”: “01”  
> 08: “01”
> 
> I do not understand why the 08s are not quoted. I tried it under ruby  
> 1.8.2 and 1.8.4.
> 
> Is this expected?

I think so. The quotes are needed to disambiguate the string “01” from  
the octal number. If quotes were not added by dump, then 01 would be  
loaded as the fixnum 1 rather than the string “01”. There’s no ambiguity  
with 08 because it is not legal octal, so it must be a string.

irb(main):004:0\> YAML.load “foo: 08”  
=\> {“foo”=\>“08”}  
irb(main):005:0\> YAML.load “foo: 07”  
=\> {“foo”=\>7}  
irb(main):006:0\> YAML.load “foo: 017”  
=\> {“foo”=\>15}

Note in the last case the conversion from octal.

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [March 23, 2006, 2:50am UTC](https://www.ruby-forum.com/t/bug-in-yaml-for-ruby/55695/4 "2006-03-23T02:50:54Z")

</div>

On Mar 22, 2006, at 7:57 PM, Matt R. wrote:

> thanks,  
> -Matt
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Well just guessing here, but you don’t have to quote 08 because it  
couldn’t be an octal number anyway, so it must be a string. I don’t  
know if this is why this happens or if this is what the YAML spec  
says but it makes sense to me, sorta.
