instance method
Psych::Visitors::YAMLTree#visit_String
visit_String(o)
ext/psych/lib/psych/visitors/yaml_tree.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/ext/psych/lib/psych/visitors/yaml_tree.rb, line 290
def visit_String o
plain = true
quote = true
style = Nodes::Scalar::PLAIN
tag = nil
if binary?(o)
o = [o].pack('m0')
tag = '!binary' # FIXME: change to below when syck is removed
#tag = 'tag:yaml.org,2002:binary'
style = Nodes::Scalar::LITERAL
plain = false
quote = false
elsif o.match?(/\n(?!\Z)/) # match \n except blank line at the end of string
style = Nodes::Scalar::LITERAL
elsif o == '<<'
style = Nodes::Scalar::SINGLE_QUOTED
tag = 'tag:yaml.org,2002:str'
plain = false
quote = false
elsif o == 'y' || o == 'Y' || o == 'n' || o == 'N'
style = Nodes::Scalar::DOUBLE_QUOTED
elsif @line_width && o.length > @line_width
style = Nodes::Scalar::FOLDED
elsif o.match?(/^[^[:word:]][^"]*$/)
style = Nodes::Scalar::DOUBLE_QUOTED
elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/.match?(o)
style = Nodes::Scalar::SINGLE_QUOTED
end
is_primitive = o.class == ::String
ivars = is_primitive ? [] : o.instance_variables
if ivars.empty?
unless is_primitive
tag = "!ruby/string:#{o.class}"
plain = false
quote = false
end
@emitter.scalar o, nil, tag, plain, quote, style
else
maptag = '!ruby/string'.dup
maptag << ":#{o.class}" unless o.class == ::String
register o, @emitter.start_mapping(nil, maptag, false, Nodes::Mapping::BLOCK)
@emitter.scalar 'str', nil, nil, true, false, Nodes::Scalar::ANY
@emitter.scalar o, nil, tag, plain, quote, style
dump_ivars o
@emitter.end_mapping
end
end
No documentation available
See also
#visit_Regexp
#visit_Struct
#visit_Rational
#visit_Symbol
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0