Skip to content

Commit b374869

Browse files
hsbtclaude
andcommitted
Resolve booleans per YAML 1.2 on the libfyaml backend
Scalar type resolution happens in ScalarScanner, not the C backend, so swapping to libfyaml alone still resolved yes/no/on/off to booleans. Key the boolean set on Psych::BACKEND so the libyaml default keeps the YAML 1.1 set while the experimental libfyaml backend follows 1.2. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 51f2493 commit b374869

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

lib/psych/scalar_scanner.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ class ScalarScanner
2424
|[-+]?(?:0|[1-9](?:[0-9]|,[0-9]|_[0-9])*) (?# base 10)
2525
|[-+]?0x[_,]*[0-9a-fA-F][0-9a-fA-F_,]* (?# base 16))$/x
2626

27+
# YAML 1.1 treats yes/no/on/off as booleans in addition to true/false,
28+
# while YAML 1.2's core schema only recognizes true/false. The default
29+
# libyaml backend keeps the 1.1 set for backward compatibility; the
30+
# experimental libfyaml backend follows 1.2.
31+
if defined?(Psych::BACKEND) && Psych::BACKEND == 'libfyaml'
32+
BOOLEAN_TRUE = /^true$/i
33+
BOOLEAN_FALSE = /^false$/i
34+
else
35+
BOOLEAN_TRUE = /^(yes|true|on)$/i
36+
BOOLEAN_FALSE = /^(no|false|off)$/i
37+
end
38+
2739
attr_reader :class_loader
2840

2941
# Create a new scanner
@@ -48,9 +60,9 @@ def tokenize string
4860
string
4961
elsif string == '~' || string.match?(/^null$/i)
5062
nil
51-
elsif string.match?(/^(yes|true|on)$/i)
63+
elsif string.match?(BOOLEAN_TRUE)
5264
true
53-
elsif string.match?(/^(no|false|off)$/i)
65+
elsif string.match?(BOOLEAN_FALSE)
5466
false
5567
else
5668
string

0 commit comments

Comments
 (0)