Module: SnippetCli::YamlScalar
- Defined in:
- lib/snippet_cli/yaml_scalar.rb
Overview
Helpers for quoting scalar values in hand-built YAML output.
Defined Under Namespace
Classes: InvalidCharacterError
Constant Summary collapse
- BOOLEAN_LIKE =
/\A(y|n|yes|no|true|false|on|off|null|~)\z/i- LEADING_SPECIAL =
/\A[:#&*!|>"%@`{}\[\]]/- CONTROL_CHARS =
Control characters that are invalid in YAML scalars (excludes tab x09, newline x0a, carriage return x0d)
/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/
Class Method Summary collapse
-
.quote(str) ⇒ Object
Quote a scalar value for YAML output.
Class Method Details
.quote(str) ⇒ Object
Quote a scalar value for YAML output. Strategy (per yaml-multiline.info):
- string containing ' → normal-quoted with escaped inner content
- string matching special YAML patterns → normal-quoted
- all other strings → single-quoted
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/snippet_cli/yaml_scalar.rb', line 18 def self.quote(str) return "''" if str.nil? || str.empty? reject_control_chars!(str) if str.include?("'") escaped = str.gsub('\\', '\\\\\\\\').gsub('"', '\\"') return "\"#{escaped}\"" end return "\"#{str.gsub('\\', '\\\\\\\\').gsub('"', '\\"')}\"" if needs_normal_quote?(str) "'#{str}'" end |