method
visit_String
v2_6_3 -
Show latest stable
- Class:
Psych::Visitors::YAMLTree
visit_String(o)public
No documentation available.
# File ext/psych/lib/psych/visitors/yaml_tree.rb, line 285
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 =~ /\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 @line_width && o.length > @line_width
style = Nodes::Scalar::FOLDED
elsif o =~ /^[^[:word:]][^"]*$/
style = Nodes::Scalar::DOUBLE_QUOTED
elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/ =~ 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