Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby30/share/ruby
File: psych.rb
# frozen_string_literal: true
[0] Fix | Delete
require 'psych/versions'
[1] Fix | Delete
case RUBY_ENGINE
[2] Fix | Delete
when 'jruby'
[3] Fix | Delete
require 'psych_jars'
[4] Fix | Delete
if JRuby::Util.respond_to?(:load_ext)
[5] Fix | Delete
JRuby::Util.load_ext('org.jruby.ext.psych.PsychLibrary')
[6] Fix | Delete
else
[7] Fix | Delete
require 'java'; require 'jruby'
[8] Fix | Delete
org.jruby.ext.psych.PsychLibrary.new.load(JRuby.runtime, false)
[9] Fix | Delete
end
[10] Fix | Delete
else
[11] Fix | Delete
require 'psych.so'
[12] Fix | Delete
end
[13] Fix | Delete
require 'psych/nodes'
[14] Fix | Delete
require 'psych/streaming'
[15] Fix | Delete
require 'psych/visitors'
[16] Fix | Delete
require 'psych/handler'
[17] Fix | Delete
require 'psych/tree_builder'
[18] Fix | Delete
require 'psych/parser'
[19] Fix | Delete
require 'psych/omap'
[20] Fix | Delete
require 'psych/set'
[21] Fix | Delete
require 'psych/coder'
[22] Fix | Delete
require 'psych/core_ext'
[23] Fix | Delete
require 'psych/stream'
[24] Fix | Delete
require 'psych/json/tree_builder'
[25] Fix | Delete
require 'psych/json/stream'
[26] Fix | Delete
require 'psych/handlers/document_stream'
[27] Fix | Delete
require 'psych/class_loader'
[28] Fix | Delete
[29] Fix | Delete
###
[30] Fix | Delete
# = Overview
[31] Fix | Delete
#
[32] Fix | Delete
# Psych is a YAML parser and emitter.
[33] Fix | Delete
# Psych leverages libyaml [Home page: https://pyyaml.org/wiki/LibYAML]
[34] Fix | Delete
# or [HG repo: https://bitbucket.org/xi/libyaml] for its YAML parsing
[35] Fix | Delete
# and emitting capabilities. In addition to wrapping libyaml, Psych also
[36] Fix | Delete
# knows how to serialize and de-serialize most Ruby objects to and from
[37] Fix | Delete
# the YAML format.
[38] Fix | Delete
#
[39] Fix | Delete
# = I NEED TO PARSE OR EMIT YAML RIGHT NOW!
[40] Fix | Delete
#
[41] Fix | Delete
# # Parse some YAML
[42] Fix | Delete
# Psych.load("--- foo") # => "foo"
[43] Fix | Delete
#
[44] Fix | Delete
# # Emit some YAML
[45] Fix | Delete
# Psych.dump("foo") # => "--- foo\n...\n"
[46] Fix | Delete
# { :a => 'b'}.to_yaml # => "---\n:a: b\n"
[47] Fix | Delete
#
[48] Fix | Delete
# Got more time on your hands? Keep on reading!
[49] Fix | Delete
#
[50] Fix | Delete
# == YAML Parsing
[51] Fix | Delete
#
[52] Fix | Delete
# Psych provides a range of interfaces for parsing a YAML document ranging from
[53] Fix | Delete
# low level to high level, depending on your parsing needs. At the lowest
[54] Fix | Delete
# level, is an event based parser. Mid level is access to the raw YAML AST,
[55] Fix | Delete
# and at the highest level is the ability to unmarshal YAML to Ruby objects.
[56] Fix | Delete
#
[57] Fix | Delete
# == YAML Emitting
[58] Fix | Delete
#
[59] Fix | Delete
# Psych provides a range of interfaces ranging from low to high level for
[60] Fix | Delete
# producing YAML documents. Very similar to the YAML parsing interfaces, Psych
[61] Fix | Delete
# provides at the lowest level, an event based system, mid-level is building
[62] Fix | Delete
# a YAML AST, and the highest level is converting a Ruby object straight to
[63] Fix | Delete
# a YAML document.
[64] Fix | Delete
#
[65] Fix | Delete
# == High-level API
[66] Fix | Delete
#
[67] Fix | Delete
# === Parsing
[68] Fix | Delete
#
[69] Fix | Delete
# The high level YAML parser provided by Psych simply takes YAML as input and
[70] Fix | Delete
# returns a Ruby data structure. For information on using the high level parser
[71] Fix | Delete
# see Psych.load
[72] Fix | Delete
#
[73] Fix | Delete
# ==== Reading from a string
[74] Fix | Delete
#
[75] Fix | Delete
# Psych.safe_load("--- a") # => 'a'
[76] Fix | Delete
# Psych.safe_load("---\n - a\n - b") # => ['a', 'b']
[77] Fix | Delete
# # From a trusted string:
[78] Fix | Delete
# Psych.load("--- !ruby/range\nbegin: 0\nend: 42\nexcl: false\n") # => 0..42
[79] Fix | Delete
#
[80] Fix | Delete
# ==== Reading from a file
[81] Fix | Delete
#
[82] Fix | Delete
# Psych.safe_load_file("data.yml", permitted_classes: [Date])
[83] Fix | Delete
# Psych.load_file("trusted_database.yml")
[84] Fix | Delete
#
[85] Fix | Delete
# ==== Exception handling
[86] Fix | Delete
#
[87] Fix | Delete
# begin
[88] Fix | Delete
# # The second argument changes only the exception contents
[89] Fix | Delete
# Psych.parse("--- `", "file.txt")
[90] Fix | Delete
# rescue Psych::SyntaxError => ex
[91] Fix | Delete
# ex.file # => 'file.txt'
[92] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[93] Fix | Delete
# end
[94] Fix | Delete
#
[95] Fix | Delete
# === Emitting
[96] Fix | Delete
#
[97] Fix | Delete
# The high level emitter has the easiest interface. Psych simply takes a Ruby
[98] Fix | Delete
# data structure and converts it to a YAML document. See Psych.dump for more
[99] Fix | Delete
# information on dumping a Ruby data structure.
[100] Fix | Delete
#
[101] Fix | Delete
# ==== Writing to a string
[102] Fix | Delete
#
[103] Fix | Delete
# # Dump an array, get back a YAML string
[104] Fix | Delete
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
[105] Fix | Delete
#
[106] Fix | Delete
# # Dump an array to an IO object
[107] Fix | Delete
# Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
[108] Fix | Delete
#
[109] Fix | Delete
# # Dump an array with indentation set
[110] Fix | Delete
# Psych.dump(['a', ['b']], :indentation => 3) # => "---\n- a\n- - b\n"
[111] Fix | Delete
#
[112] Fix | Delete
# # Dump an array to an IO with indentation set
[113] Fix | Delete
# Psych.dump(['a', ['b']], StringIO.new, :indentation => 3)
[114] Fix | Delete
#
[115] Fix | Delete
# ==== Writing to a file
[116] Fix | Delete
#
[117] Fix | Delete
# Currently there is no direct API for dumping Ruby structure to file:
[118] Fix | Delete
#
[119] Fix | Delete
# File.open('database.yml', 'w') do |file|
[120] Fix | Delete
# file.write(Psych.dump(['a', 'b']))
[121] Fix | Delete
# end
[122] Fix | Delete
#
[123] Fix | Delete
# == Mid-level API
[124] Fix | Delete
#
[125] Fix | Delete
# === Parsing
[126] Fix | Delete
#
[127] Fix | Delete
# Psych provides access to an AST produced from parsing a YAML document. This
[128] Fix | Delete
# tree is built using the Psych::Parser and Psych::TreeBuilder. The AST can
[129] Fix | Delete
# be examined and manipulated freely. Please see Psych::parse_stream,
[130] Fix | Delete
# Psych::Nodes, and Psych::Nodes::Node for more information on dealing with
[131] Fix | Delete
# YAML syntax trees.
[132] Fix | Delete
#
[133] Fix | Delete
# ==== Reading from a string
[134] Fix | Delete
#
[135] Fix | Delete
# # Returns Psych::Nodes::Stream
[136] Fix | Delete
# Psych.parse_stream("---\n - a\n - b")
[137] Fix | Delete
#
[138] Fix | Delete
# # Returns Psych::Nodes::Document
[139] Fix | Delete
# Psych.parse("---\n - a\n - b")
[140] Fix | Delete
#
[141] Fix | Delete
# ==== Reading from a file
[142] Fix | Delete
#
[143] Fix | Delete
# # Returns Psych::Nodes::Stream
[144] Fix | Delete
# Psych.parse_stream(File.read('database.yml'))
[145] Fix | Delete
#
[146] Fix | Delete
# # Returns Psych::Nodes::Document
[147] Fix | Delete
# Psych.parse_file('database.yml')
[148] Fix | Delete
#
[149] Fix | Delete
# ==== Exception handling
[150] Fix | Delete
#
[151] Fix | Delete
# begin
[152] Fix | Delete
# # The second argument changes only the exception contents
[153] Fix | Delete
# Psych.parse("--- `", "file.txt")
[154] Fix | Delete
# rescue Psych::SyntaxError => ex
[155] Fix | Delete
# ex.file # => 'file.txt'
[156] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[157] Fix | Delete
# end
[158] Fix | Delete
#
[159] Fix | Delete
# === Emitting
[160] Fix | Delete
#
[161] Fix | Delete
# At the mid level is building an AST. This AST is exactly the same as the AST
[162] Fix | Delete
# used when parsing a YAML document. Users can build an AST by hand and the
[163] Fix | Delete
# AST knows how to emit itself as a YAML document. See Psych::Nodes,
[164] Fix | Delete
# Psych::Nodes::Node, and Psych::TreeBuilder for more information on building
[165] Fix | Delete
# a YAML AST.
[166] Fix | Delete
#
[167] Fix | Delete
# ==== Writing to a string
[168] Fix | Delete
#
[169] Fix | Delete
# # We need Psych::Nodes::Stream (not Psych::Nodes::Document)
[170] Fix | Delete
# stream = Psych.parse_stream("---\n - a\n - b")
[171] Fix | Delete
#
[172] Fix | Delete
# stream.to_yaml # => "---\n- a\n- b\n"
[173] Fix | Delete
#
[174] Fix | Delete
# ==== Writing to a file
[175] Fix | Delete
#
[176] Fix | Delete
# # We need Psych::Nodes::Stream (not Psych::Nodes::Document)
[177] Fix | Delete
# stream = Psych.parse_stream(File.read('database.yml'))
[178] Fix | Delete
#
[179] Fix | Delete
# File.open('database.yml', 'w') do |file|
[180] Fix | Delete
# file.write(stream.to_yaml)
[181] Fix | Delete
# end
[182] Fix | Delete
#
[183] Fix | Delete
# == Low-level API
[184] Fix | Delete
#
[185] Fix | Delete
# === Parsing
[186] Fix | Delete
#
[187] Fix | Delete
# The lowest level parser should be used when the YAML input is already known,
[188] Fix | Delete
# and the developer does not want to pay the price of building an AST or
[189] Fix | Delete
# automatic detection and conversion to Ruby objects. See Psych::Parser for
[190] Fix | Delete
# more information on using the event based parser.
[191] Fix | Delete
#
[192] Fix | Delete
# ==== Reading to Psych::Nodes::Stream structure
[193] Fix | Delete
#
[194] Fix | Delete
# parser = Psych::Parser.new(TreeBuilder.new) # => #<Psych::Parser>
[195] Fix | Delete
# parser = Psych.parser # it's an alias for the above
[196] Fix | Delete
#
[197] Fix | Delete
# parser.parse("---\n - a\n - b") # => #<Psych::Parser>
[198] Fix | Delete
# parser.handler # => #<Psych::TreeBuilder>
[199] Fix | Delete
# parser.handler.root # => #<Psych::Nodes::Stream>
[200] Fix | Delete
#
[201] Fix | Delete
# ==== Receiving an events stream
[202] Fix | Delete
#
[203] Fix | Delete
# recorder = Psych::Handlers::Recorder.new
[204] Fix | Delete
# parser = Psych::Parser.new(recorder)
[205] Fix | Delete
#
[206] Fix | Delete
# parser.parse("---\n - a\n - b")
[207] Fix | Delete
# recorder.events # => [list of [event, args] lists]
[208] Fix | Delete
# # event is one of: Psych::Handler::EVENTS
[209] Fix | Delete
# # args are the arguments passed to the event
[210] Fix | Delete
#
[211] Fix | Delete
# === Emitting
[212] Fix | Delete
#
[213] Fix | Delete
# The lowest level emitter is an event based system. Events are sent to a
[214] Fix | Delete
# Psych::Emitter object. That object knows how to convert the events to a YAML
[215] Fix | Delete
# document. This interface should be used when document format is known in
[216] Fix | Delete
# advance or speed is a concern. See Psych::Emitter for more information.
[217] Fix | Delete
#
[218] Fix | Delete
# ==== Writing to a Ruby structure
[219] Fix | Delete
#
[220] Fix | Delete
# Psych.parser.parse("--- a") # => #<Psych::Parser>
[221] Fix | Delete
#
[222] Fix | Delete
# parser.handler.first # => #<Psych::Nodes::Stream>
[223] Fix | Delete
# parser.handler.first.to_ruby # => ["a"]
[224] Fix | Delete
#
[225] Fix | Delete
# parser.handler.root.first # => #<Psych::Nodes::Document>
[226] Fix | Delete
# parser.handler.root.first.to_ruby # => "a"
[227] Fix | Delete
#
[228] Fix | Delete
# # You can instantiate an Emitter manually
[229] Fix | Delete
# Psych::Visitors::ToRuby.new.accept(parser.handler.root.first)
[230] Fix | Delete
# # => "a"
[231] Fix | Delete
[232] Fix | Delete
module Psych
[233] Fix | Delete
# The version of libyaml Psych is using
[234] Fix | Delete
LIBYAML_VERSION = Psych.libyaml_version.join('.').freeze
[235] Fix | Delete
# Deprecation guard
[236] Fix | Delete
NOT_GIVEN = Object.new.freeze
[237] Fix | Delete
private_constant :NOT_GIVEN
[238] Fix | Delete
[239] Fix | Delete
###
[240] Fix | Delete
# Load +yaml+ in to a Ruby data structure. If multiple documents are
[241] Fix | Delete
# provided, the object contained in the first document will be returned.
[242] Fix | Delete
# +filename+ will be used in the exception message if any exception
[243] Fix | Delete
# is raised while parsing. If +yaml+ is empty, it returns
[244] Fix | Delete
# the specified +fallback+ return value, which defaults to +false+.
[245] Fix | Delete
#
[246] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[247] Fix | Delete
#
[248] Fix | Delete
# Example:
[249] Fix | Delete
#
[250] Fix | Delete
# Psych.load("--- a") # => 'a'
[251] Fix | Delete
# Psych.load("---\n - a\n - b") # => ['a', 'b']
[252] Fix | Delete
#
[253] Fix | Delete
# begin
[254] Fix | Delete
# Psych.load("--- `", filename: "file.txt")
[255] Fix | Delete
# rescue Psych::SyntaxError => ex
[256] Fix | Delete
# ex.file # => 'file.txt'
[257] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[258] Fix | Delete
# end
[259] Fix | Delete
#
[260] Fix | Delete
# When the optional +symbolize_names+ keyword argument is set to a
[261] Fix | Delete
# true value, returns symbols for keys in Hash objects (default: strings).
[262] Fix | Delete
#
[263] Fix | Delete
# Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
[264] Fix | Delete
# Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
[265] Fix | Delete
#
[266] Fix | Delete
# Raises a TypeError when `yaml` parameter is NilClass
[267] Fix | Delete
#
[268] Fix | Delete
# NOTE: This method *should not* be used to parse untrusted documents, such as
[269] Fix | Delete
# YAML documents that are supplied via user input. Instead, please use the
[270] Fix | Delete
# safe_load method.
[271] Fix | Delete
#
[272] Fix | Delete
def self.unsafe_load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false, symbolize_names: false, freeze: false
[273] Fix | Delete
if legacy_filename != NOT_GIVEN
[274] Fix | Delete
warn_with_uplevel 'Passing filename with the 2nd argument of Psych.load is deprecated. Use keyword argument like Psych.load(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
[275] Fix | Delete
filename = legacy_filename
[276] Fix | Delete
end
[277] Fix | Delete
[278] Fix | Delete
result = parse(yaml, filename: filename)
[279] Fix | Delete
return fallback unless result
[280] Fix | Delete
result.to_ruby(symbolize_names: symbolize_names, freeze: freeze)
[281] Fix | Delete
end
[282] Fix | Delete
class << self; alias :load :unsafe_load; end
[283] Fix | Delete
[284] Fix | Delete
###
[285] Fix | Delete
# Safely load the yaml string in +yaml+. By default, only the following
[286] Fix | Delete
# classes are allowed to be deserialized:
[287] Fix | Delete
#
[288] Fix | Delete
# * TrueClass
[289] Fix | Delete
# * FalseClass
[290] Fix | Delete
# * NilClass
[291] Fix | Delete
# * Numeric
[292] Fix | Delete
# * String
[293] Fix | Delete
# * Array
[294] Fix | Delete
# * Hash
[295] Fix | Delete
#
[296] Fix | Delete
# Recursive data structures are not allowed by default. Arbitrary classes
[297] Fix | Delete
# can be allowed by adding those classes to the +permitted_classes+ keyword argument. They are
[298] Fix | Delete
# additive. For example, to allow Date deserialization:
[299] Fix | Delete
#
[300] Fix | Delete
# Psych.safe_load(yaml, permitted_classes: [Date])
[301] Fix | Delete
#
[302] Fix | Delete
# Now the Date class can be loaded in addition to the classes listed above.
[303] Fix | Delete
#
[304] Fix | Delete
# Aliases can be explicitly allowed by changing the +aliases+ keyword argument.
[305] Fix | Delete
# For example:
[306] Fix | Delete
#
[307] Fix | Delete
# x = []
[308] Fix | Delete
# x << x
[309] Fix | Delete
# yaml = Psych.dump x
[310] Fix | Delete
# Psych.safe_load yaml # => raises an exception
[311] Fix | Delete
# Psych.safe_load yaml, aliases: true # => loads the aliases
[312] Fix | Delete
#
[313] Fix | Delete
# A Psych::DisallowedClass exception will be raised if the yaml contains a
[314] Fix | Delete
# class that isn't in the +permitted_classes+ list.
[315] Fix | Delete
#
[316] Fix | Delete
# A Psych::BadAlias exception will be raised if the yaml contains aliases
[317] Fix | Delete
# but the +aliases+ keyword argument is set to false.
[318] Fix | Delete
#
[319] Fix | Delete
# +filename+ will be used in the exception message if any exception is raised
[320] Fix | Delete
# while parsing.
[321] Fix | Delete
#
[322] Fix | Delete
# When the optional +symbolize_names+ keyword argument is set to a
[323] Fix | Delete
# true value, returns symbols for keys in Hash objects (default: strings).
[324] Fix | Delete
#
[325] Fix | Delete
# Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
[326] Fix | Delete
# Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
[327] Fix | Delete
#
[328] Fix | Delete
def self.safe_load yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false
[329] Fix | Delete
if legacy_permitted_classes != NOT_GIVEN
[330] Fix | Delete
warn_with_uplevel 'Passing permitted_classes with the 2nd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_classes: ...) instead.', uplevel: 1 if $VERBOSE
[331] Fix | Delete
permitted_classes = legacy_permitted_classes
[332] Fix | Delete
end
[333] Fix | Delete
[334] Fix | Delete
if legacy_permitted_symbols != NOT_GIVEN
[335] Fix | Delete
warn_with_uplevel 'Passing permitted_symbols with the 3rd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_symbols: ...) instead.', uplevel: 1 if $VERBOSE
[336] Fix | Delete
permitted_symbols = legacy_permitted_symbols
[337] Fix | Delete
end
[338] Fix | Delete
[339] Fix | Delete
if legacy_aliases != NOT_GIVEN
[340] Fix | Delete
warn_with_uplevel 'Passing aliases with the 4th argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, aliases: ...) instead.', uplevel: 1 if $VERBOSE
[341] Fix | Delete
aliases = legacy_aliases
[342] Fix | Delete
end
[343] Fix | Delete
[344] Fix | Delete
if legacy_filename != NOT_GIVEN
[345] Fix | Delete
warn_with_uplevel 'Passing filename with the 5th argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
[346] Fix | Delete
filename = legacy_filename
[347] Fix | Delete
end
[348] Fix | Delete
[349] Fix | Delete
result = parse(yaml, filename: filename)
[350] Fix | Delete
return fallback unless result
[351] Fix | Delete
[352] Fix | Delete
class_loader = ClassLoader::Restricted.new(permitted_classes.map(&:to_s),
[353] Fix | Delete
permitted_symbols.map(&:to_s))
[354] Fix | Delete
scanner = ScalarScanner.new class_loader
[355] Fix | Delete
visitor = if aliases
[356] Fix | Delete
Visitors::ToRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze
[357] Fix | Delete
else
[358] Fix | Delete
Visitors::NoAliasRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze
[359] Fix | Delete
end
[360] Fix | Delete
result = visitor.accept result
[361] Fix | Delete
result
[362] Fix | Delete
end
[363] Fix | Delete
[364] Fix | Delete
###
[365] Fix | Delete
# Parse a YAML string in +yaml+. Returns the Psych::Nodes::Document.
[366] Fix | Delete
# +filename+ is used in the exception message if a Psych::SyntaxError is
[367] Fix | Delete
# raised.
[368] Fix | Delete
#
[369] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[370] Fix | Delete
#
[371] Fix | Delete
# Example:
[372] Fix | Delete
#
[373] Fix | Delete
# Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Document:0x00>
[374] Fix | Delete
#
[375] Fix | Delete
# begin
[376] Fix | Delete
# Psych.parse("--- `", filename: "file.txt")
[377] Fix | Delete
# rescue Psych::SyntaxError => ex
[378] Fix | Delete
# ex.file # => 'file.txt'
[379] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[380] Fix | Delete
# end
[381] Fix | Delete
#
[382] Fix | Delete
# See Psych::Nodes for more information about YAML AST.
[383] Fix | Delete
def self.parse yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: NOT_GIVEN
[384] Fix | Delete
if legacy_filename != NOT_GIVEN
[385] Fix | Delete
warn_with_uplevel 'Passing filename with the 2nd argument of Psych.parse is deprecated. Use keyword argument like Psych.parse(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
[386] Fix | Delete
filename = legacy_filename
[387] Fix | Delete
end
[388] Fix | Delete
[389] Fix | Delete
parse_stream(yaml, filename: filename) do |node|
[390] Fix | Delete
return node
[391] Fix | Delete
end
[392] Fix | Delete
[393] Fix | Delete
if fallback != NOT_GIVEN
[394] Fix | Delete
warn_with_uplevel 'Passing the `fallback` keyword argument of Psych.parse is deprecated.', uplevel: 1 if $VERBOSE
[395] Fix | Delete
fallback
[396] Fix | Delete
else
[397] Fix | Delete
false
[398] Fix | Delete
end
[399] Fix | Delete
end
[400] Fix | Delete
[401] Fix | Delete
###
[402] Fix | Delete
# Parse a file at +filename+. Returns the Psych::Nodes::Document.
[403] Fix | Delete
#
[404] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[405] Fix | Delete
def self.parse_file filename, fallback: false
[406] Fix | Delete
result = File.open filename, 'r:bom|utf-8' do |f|
[407] Fix | Delete
parse f, filename: filename
[408] Fix | Delete
end
[409] Fix | Delete
result || fallback
[410] Fix | Delete
end
[411] Fix | Delete
[412] Fix | Delete
###
[413] Fix | Delete
# Returns a default parser
[414] Fix | Delete
def self.parser
[415] Fix | Delete
Psych::Parser.new(TreeBuilder.new)
[416] Fix | Delete
end
[417] Fix | Delete
[418] Fix | Delete
###
[419] Fix | Delete
# Parse a YAML string in +yaml+. Returns the Psych::Nodes::Stream.
[420] Fix | Delete
# This method can handle multiple YAML documents contained in +yaml+.
[421] Fix | Delete
# +filename+ is used in the exception message if a Psych::SyntaxError is
[422] Fix | Delete
# raised.
[423] Fix | Delete
#
[424] Fix | Delete
# If a block is given, a Psych::Nodes::Document node will be yielded to the
[425] Fix | Delete
# block as it's being parsed.
[426] Fix | Delete
#
[427] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[428] Fix | Delete
#
[429] Fix | Delete
# Example:
[430] Fix | Delete
#
[431] Fix | Delete
# Psych.parse_stream("---\n - a\n - b") # => #<Psych::Nodes::Stream:0x00>
[432] Fix | Delete
#
[433] Fix | Delete
# Psych.parse_stream("--- a\n--- b") do |node|
[434] Fix | Delete
# node # => #<Psych::Nodes::Document:0x00>
[435] Fix | Delete
# end
[436] Fix | Delete
#
[437] Fix | Delete
# begin
[438] Fix | Delete
# Psych.parse_stream("--- `", filename: "file.txt")
[439] Fix | Delete
# rescue Psych::SyntaxError => ex
[440] Fix | Delete
# ex.file # => 'file.txt'
[441] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[442] Fix | Delete
# end
[443] Fix | Delete
#
[444] Fix | Delete
# Raises a TypeError when NilClass is passed.
[445] Fix | Delete
#
[446] Fix | Delete
# See Psych::Nodes for more information about YAML AST.
[447] Fix | Delete
def self.parse_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, &block
[448] Fix | Delete
if legacy_filename != NOT_GIVEN
[449] Fix | Delete
warn_with_uplevel 'Passing filename with the 2nd argument of Psych.parse_stream is deprecated. Use keyword argument like Psych.parse_stream(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
[450] Fix | Delete
filename = legacy_filename
[451] Fix | Delete
end
[452] Fix | Delete
[453] Fix | Delete
if block_given?
[454] Fix | Delete
parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
[455] Fix | Delete
parser.parse yaml, filename
[456] Fix | Delete
else
[457] Fix | Delete
parser = self.parser
[458] Fix | Delete
parser.parse yaml, filename
[459] Fix | Delete
parser.handler.root
[460] Fix | Delete
end
[461] Fix | Delete
end
[462] Fix | Delete
[463] Fix | Delete
###
[464] Fix | Delete
# call-seq:
[465] Fix | Delete
# Psych.dump(o) -> string of yaml
[466] Fix | Delete
# Psych.dump(o, options) -> string of yaml
[467] Fix | Delete
# Psych.dump(o, io) -> io object passed in
[468] Fix | Delete
# Psych.dump(o, io, options) -> io object passed in
[469] Fix | Delete
#
[470] Fix | Delete
# Dump Ruby object +o+ to a YAML string. Optional +options+ may be passed in
[471] Fix | Delete
# to control the output format. If an IO object is passed in, the YAML will
[472] Fix | Delete
# be dumped to that IO object.
[473] Fix | Delete
#
[474] Fix | Delete
# Currently supported options are:
[475] Fix | Delete
#
[476] Fix | Delete
# [<tt>:indentation</tt>] Number of space characters used to indent.
[477] Fix | Delete
# Acceptable value should be in <tt>0..9</tt> range,
[478] Fix | Delete
# otherwise option is ignored.
[479] Fix | Delete
#
[480] Fix | Delete
# Default: <tt>2</tt>.
[481] Fix | Delete
# [<tt>:line_width</tt>] Max character to wrap line at.
[482] Fix | Delete
#
[483] Fix | Delete
# Default: <tt>0</tt> (meaning "wrap at 81").
[484] Fix | Delete
# [<tt>:canonical</tt>] Write "canonical" YAML form (very verbose, yet
[485] Fix | Delete
# strictly formal).
[486] Fix | Delete
#
[487] Fix | Delete
# Default: <tt>false</tt>.
[488] Fix | Delete
# [<tt>:header</tt>] Write <tt>%YAML [version]</tt> at the beginning of document.
[489] Fix | Delete
#
[490] Fix | Delete
# Default: <tt>false</tt>.
[491] Fix | Delete
#
[492] Fix | Delete
# Example:
[493] Fix | Delete
#
[494] Fix | Delete
# # Dump an array, get back a YAML string
[495] Fix | Delete
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
[496] Fix | Delete
#
[497] Fix | Delete
# # Dump an array to an IO object
[498] Fix | Delete
# Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function