Skip to content
  • Godfrey Chan's avatar
    debe7aed
    Properly dump primitive-like AS::SafeBuffer strings as YAML · debe7aed
    Godfrey Chan authored
    `coder.represent_scalar` means something along the lines of "Here is a quoted
    string, you can just add it to the output", which is not the case here. It only
    works for simple strings that can appear unquoted in YAML, but causes problems
    for e.g. primitive-like strings ("1", "true").
    
    `coder.represent_object` on the other hand, means that "This is the Ruby-object
    representation for this thing suitable for use in YAML dumping", which is what
    we want here.
    
    Before:
    
       YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml  # => "Hello"
       YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml   # => true
       YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml  # => false
       YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml      # => 1
       YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml    # => 1.1
    
     After:
    
       YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml  # => "Hello"
       YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml   # => "true"
       YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml  # => "false"
       YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml      # => "1"
       YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml    # => "1.1"
    
    If we ever want Ruby to behave more like PHP or JavaScript though, this is an
    excellent trick to use ;)
    debe7aed
    Properly dump primitive-like AS::SafeBuffer strings as YAML
    Godfrey Chan authored
    `coder.represent_scalar` means something along the lines of "Here is a quoted
    string, you can just add it to the output", which is not the case here. It only
    works for simple strings that can appear unquoted in YAML, but causes problems
    for e.g. primitive-like strings ("1", "true").
    
    `coder.represent_object` on the other hand, means that "This is the Ruby-object
    representation for this thing suitable for use in YAML dumping", which is what
    we want here.
    
    Before:
    
       YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml  # => "Hello"
       YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml   # => true
       YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml  # => false
       YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml      # => 1
       YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml    # => 1.1
    
     After:
    
       YAML.load ActiveSupport::SafeBuffer.new("Hello").to_yaml  # => "Hello"
       YAML.load ActiveSupport::SafeBuffer.new("true").to_yaml   # => "true"
       YAML.load ActiveSupport::SafeBuffer.new("false").to_yaml  # => "false"
       YAML.load ActiveSupport::SafeBuffer.new("1").to_yaml      # => "1"
       YAML.load ActiveSupport::SafeBuffer.new("1.1").to_yaml    # => "1.1"
    
    If we ever want Ruby to behave more like PHP or JavaScript though, this is an
    excellent trick to use ;)
Loading