Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: psych.rb
# frozen_string_literal: true
[0] Fix | Delete
require_relative 'psych/versions'
[1] Fix | Delete
case RUBY_ENGINE
[2] Fix | Delete
when 'jruby'
[3] Fix | Delete
require_relative '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_relative 'psych/nodes'
[14] Fix | Delete
require_relative 'psych/streaming'
[15] Fix | Delete
require_relative 'psych/visitors'
[16] Fix | Delete
require_relative 'psych/handler'
[17] Fix | Delete
require_relative 'psych/tree_builder'
[18] Fix | Delete
require_relative 'psych/parser'
[19] Fix | Delete
require_relative 'psych/omap'
[20] Fix | Delete
require_relative 'psych/set'
[21] Fix | Delete
require_relative 'psych/coder'
[22] Fix | Delete
require_relative 'psych/core_ext'
[23] Fix | Delete
require_relative 'psych/stream'
[24] Fix | Delete
require_relative 'psych/json/tree_builder'
[25] Fix | Delete
require_relative 'psych/json/stream'
[26] Fix | Delete
require_relative 'psych/handlers/document_stream'
[27] Fix | Delete
require_relative '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 [git repo: https://github.com/yaml/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
[236] Fix | Delete
###
[237] Fix | Delete
# Load +yaml+ in to a Ruby data structure. If multiple documents are
[238] Fix | Delete
# provided, the object contained in the first document will be returned.
[239] Fix | Delete
# +filename+ will be used in the exception message if any exception
[240] Fix | Delete
# is raised while parsing. If +yaml+ is empty, it returns
[241] Fix | Delete
# the specified +fallback+ return value, which defaults to +false+.
[242] Fix | Delete
#
[243] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[244] Fix | Delete
#
[245] Fix | Delete
# Example:
[246] Fix | Delete
#
[247] Fix | Delete
# Psych.unsafe_load("--- a") # => 'a'
[248] Fix | Delete
# Psych.unsafe_load("---\n - a\n - b") # => ['a', 'b']
[249] Fix | Delete
#
[250] Fix | Delete
# begin
[251] Fix | Delete
# Psych.unsafe_load("--- `", filename: "file.txt")
[252] Fix | Delete
# rescue Psych::SyntaxError => ex
[253] Fix | Delete
# ex.file # => 'file.txt'
[254] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[255] Fix | Delete
# end
[256] Fix | Delete
#
[257] Fix | Delete
# When the optional +symbolize_names+ keyword argument is set to a
[258] Fix | Delete
# true value, returns symbols for keys in Hash objects (default: strings).
[259] Fix | Delete
#
[260] Fix | Delete
# Psych.unsafe_load("---\n foo: bar") # => {"foo"=>"bar"}
[261] Fix | Delete
# Psych.unsafe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
[262] Fix | Delete
#
[263] Fix | Delete
# Raises a TypeError when `yaml` parameter is NilClass
[264] Fix | Delete
#
[265] Fix | Delete
# NOTE: This method *should not* be used to parse untrusted documents, such as
[266] Fix | Delete
# YAML documents that are supplied via user input. Instead, please use the
[267] Fix | Delete
# load method or the safe_load method.
[268] Fix | Delete
#
[269] Fix | Delete
def self.unsafe_load yaml, filename: nil, fallback: false, symbolize_names: false, freeze: false, strict_integer: false
[270] Fix | Delete
result = parse(yaml, filename: filename)
[271] Fix | Delete
return fallback unless result
[272] Fix | Delete
result.to_ruby(symbolize_names: symbolize_names, freeze: freeze, strict_integer: strict_integer)
[273] Fix | Delete
end
[274] Fix | Delete
[275] Fix | Delete
###
[276] Fix | Delete
# Safely load the yaml string in +yaml+. By default, only the following
[277] Fix | Delete
# classes are allowed to be deserialized:
[278] Fix | Delete
#
[279] Fix | Delete
# * TrueClass
[280] Fix | Delete
# * FalseClass
[281] Fix | Delete
# * NilClass
[282] Fix | Delete
# * Integer
[283] Fix | Delete
# * Float
[284] Fix | Delete
# * String
[285] Fix | Delete
# * Array
[286] Fix | Delete
# * Hash
[287] Fix | Delete
#
[288] Fix | Delete
# Recursive data structures are not allowed by default. Arbitrary classes
[289] Fix | Delete
# can be allowed by adding those classes to the +permitted_classes+ keyword argument. They are
[290] Fix | Delete
# additive. For example, to allow Date deserialization:
[291] Fix | Delete
#
[292] Fix | Delete
# Psych.safe_load(yaml, permitted_classes: [Date])
[293] Fix | Delete
#
[294] Fix | Delete
# Now the Date class can be loaded in addition to the classes listed above.
[295] Fix | Delete
#
[296] Fix | Delete
# Aliases can be explicitly allowed by changing the +aliases+ keyword argument.
[297] Fix | Delete
# For example:
[298] Fix | Delete
#
[299] Fix | Delete
# x = []
[300] Fix | Delete
# x << x
[301] Fix | Delete
# yaml = Psych.dump x
[302] Fix | Delete
# Psych.safe_load yaml # => raises an exception
[303] Fix | Delete
# Psych.safe_load yaml, aliases: true # => loads the aliases
[304] Fix | Delete
#
[305] Fix | Delete
# A Psych::DisallowedClass exception will be raised if the yaml contains a
[306] Fix | Delete
# class that isn't in the +permitted_classes+ list.
[307] Fix | Delete
#
[308] Fix | Delete
# A Psych::BadAlias exception will be raised if the yaml contains aliases
[309] Fix | Delete
# but the +aliases+ keyword argument is set to false.
[310] Fix | Delete
#
[311] Fix | Delete
# +filename+ will be used in the exception message if any exception is raised
[312] Fix | Delete
# while parsing.
[313] Fix | Delete
#
[314] Fix | Delete
# When the optional +symbolize_names+ keyword argument is set to a
[315] Fix | Delete
# true value, returns symbols for keys in Hash objects (default: strings).
[316] Fix | Delete
#
[317] Fix | Delete
# Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
[318] Fix | Delete
# Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
[319] Fix | Delete
#
[320] Fix | Delete
def self.safe_load yaml, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false, strict_integer: false
[321] Fix | Delete
result = parse(yaml, filename: filename)
[322] Fix | Delete
return fallback unless result
[323] Fix | Delete
[324] Fix | Delete
class_loader = ClassLoader::Restricted.new(permitted_classes.map(&:to_s),
[325] Fix | Delete
permitted_symbols.map(&:to_s))
[326] Fix | Delete
scanner = ScalarScanner.new class_loader, strict_integer: strict_integer
[327] Fix | Delete
visitor = if aliases
[328] Fix | Delete
Visitors::ToRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze
[329] Fix | Delete
else
[330] Fix | Delete
Visitors::NoAliasRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze
[331] Fix | Delete
end
[332] Fix | Delete
result = visitor.accept result
[333] Fix | Delete
result
[334] Fix | Delete
end
[335] Fix | Delete
[336] Fix | Delete
###
[337] Fix | Delete
# Load +yaml+ in to a Ruby data structure. If multiple documents are
[338] Fix | Delete
# provided, the object contained in the first document will be returned.
[339] Fix | Delete
# +filename+ will be used in the exception message if any exception
[340] Fix | Delete
# is raised while parsing. If +yaml+ is empty, it returns
[341] Fix | Delete
# the specified +fallback+ return value, which defaults to +false+.
[342] Fix | Delete
#
[343] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[344] Fix | Delete
#
[345] Fix | Delete
# Example:
[346] Fix | Delete
#
[347] Fix | Delete
# Psych.load("--- a") # => 'a'
[348] Fix | Delete
# Psych.load("---\n - a\n - b") # => ['a', 'b']
[349] Fix | Delete
#
[350] Fix | Delete
# begin
[351] Fix | Delete
# Psych.load("--- `", filename: "file.txt")
[352] Fix | Delete
# rescue Psych::SyntaxError => ex
[353] Fix | Delete
# ex.file # => 'file.txt'
[354] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[355] Fix | Delete
# end
[356] Fix | Delete
#
[357] Fix | Delete
# When the optional +symbolize_names+ keyword argument is set to a
[358] Fix | Delete
# true value, returns symbols for keys in Hash objects (default: strings).
[359] Fix | Delete
#
[360] Fix | Delete
# Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
[361] Fix | Delete
# Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
[362] Fix | Delete
#
[363] Fix | Delete
# Raises a TypeError when `yaml` parameter is NilClass. This method is
[364] Fix | Delete
# similar to `safe_load` except that `Symbol` objects are allowed by default.
[365] Fix | Delete
#
[366] Fix | Delete
def self.load yaml, permitted_classes: [Symbol], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false, strict_integer: false
[367] Fix | Delete
safe_load yaml, permitted_classes: permitted_classes,
[368] Fix | Delete
permitted_symbols: permitted_symbols,
[369] Fix | Delete
aliases: aliases,
[370] Fix | Delete
filename: filename,
[371] Fix | Delete
fallback: fallback,
[372] Fix | Delete
symbolize_names: symbolize_names,
[373] Fix | Delete
freeze: freeze,
[374] Fix | Delete
strict_integer: strict_integer
[375] Fix | Delete
end
[376] Fix | Delete
[377] Fix | Delete
###
[378] Fix | Delete
# Parse a YAML string in +yaml+. Returns the Psych::Nodes::Document.
[379] Fix | Delete
# +filename+ is used in the exception message if a Psych::SyntaxError is
[380] Fix | Delete
# raised.
[381] Fix | Delete
#
[382] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[383] Fix | Delete
#
[384] Fix | Delete
# Example:
[385] Fix | Delete
#
[386] Fix | Delete
# Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Document:0x00>
[387] Fix | Delete
#
[388] Fix | Delete
# begin
[389] Fix | Delete
# Psych.parse("--- `", filename: "file.txt")
[390] Fix | Delete
# rescue Psych::SyntaxError => ex
[391] Fix | Delete
# ex.file # => 'file.txt'
[392] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[393] Fix | Delete
# end
[394] Fix | Delete
#
[395] Fix | Delete
# See Psych::Nodes for more information about YAML AST.
[396] Fix | Delete
def self.parse yaml, filename: nil
[397] Fix | Delete
parse_stream(yaml, filename: filename) do |node|
[398] Fix | Delete
return node
[399] Fix | Delete
end
[400] Fix | Delete
[401] Fix | Delete
false
[402] Fix | Delete
end
[403] Fix | Delete
[404] Fix | Delete
###
[405] Fix | Delete
# Parse a file at +filename+. Returns the Psych::Nodes::Document.
[406] Fix | Delete
#
[407] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[408] Fix | Delete
def self.parse_file filename, fallback: false
[409] Fix | Delete
result = File.open filename, 'r:bom|utf-8' do |f|
[410] Fix | Delete
parse f, filename: filename
[411] Fix | Delete
end
[412] Fix | Delete
result || fallback
[413] Fix | Delete
end
[414] Fix | Delete
[415] Fix | Delete
###
[416] Fix | Delete
# Returns a default parser
[417] Fix | Delete
def self.parser
[418] Fix | Delete
Psych::Parser.new(TreeBuilder.new)
[419] Fix | Delete
end
[420] Fix | Delete
[421] Fix | Delete
###
[422] Fix | Delete
# Parse a YAML string in +yaml+. Returns the Psych::Nodes::Stream.
[423] Fix | Delete
# This method can handle multiple YAML documents contained in +yaml+.
[424] Fix | Delete
# +filename+ is used in the exception message if a Psych::SyntaxError is
[425] Fix | Delete
# raised.
[426] Fix | Delete
#
[427] Fix | Delete
# If a block is given, a Psych::Nodes::Document node will be yielded to the
[428] Fix | Delete
# block as it's being parsed.
[429] Fix | Delete
#
[430] Fix | Delete
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
[431] Fix | Delete
#
[432] Fix | Delete
# Example:
[433] Fix | Delete
#
[434] Fix | Delete
# Psych.parse_stream("---\n - a\n - b") # => #<Psych::Nodes::Stream:0x00>
[435] Fix | Delete
#
[436] Fix | Delete
# Psych.parse_stream("--- a\n--- b") do |node|
[437] Fix | Delete
# node # => #<Psych::Nodes::Document:0x00>
[438] Fix | Delete
# end
[439] Fix | Delete
#
[440] Fix | Delete
# begin
[441] Fix | Delete
# Psych.parse_stream("--- `", filename: "file.txt")
[442] Fix | Delete
# rescue Psych::SyntaxError => ex
[443] Fix | Delete
# ex.file # => 'file.txt'
[444] Fix | Delete
# ex.message # => "(file.txt): found character that cannot start any token"
[445] Fix | Delete
# end
[446] Fix | Delete
#
[447] Fix | Delete
# Raises a TypeError when NilClass is passed.
[448] Fix | Delete
#
[449] Fix | Delete
# See Psych::Nodes for more information about YAML AST.
[450] Fix | Delete
def self.parse_stream yaml, filename: nil, &block
[451] Fix | Delete
if block_given?
[452] Fix | Delete
parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
[453] Fix | Delete
parser.parse yaml, filename
[454] Fix | Delete
else
[455] Fix | Delete
parser = self.parser
[456] Fix | Delete
parser.parse yaml, filename
[457] Fix | Delete
parser.handler.root
[458] Fix | Delete
end
[459] Fix | Delete
end
[460] Fix | Delete
[461] Fix | Delete
###
[462] Fix | Delete
# call-seq:
[463] Fix | Delete
# Psych.dump(o) -> string of yaml
[464] Fix | Delete
# Psych.dump(o, options) -> string of yaml
[465] Fix | Delete
# Psych.dump(o, io) -> io object passed in
[466] Fix | Delete
# Psych.dump(o, io, options) -> io object passed in
[467] Fix | Delete
#
[468] Fix | Delete
# Dump Ruby object +o+ to a YAML string. Optional +options+ may be passed in
[469] Fix | Delete
# to control the output format. If an IO object is passed in, the YAML will
[470] Fix | Delete
# be dumped to that IO object.
[471] Fix | Delete
#
[472] Fix | Delete
# Currently supported options are:
[473] Fix | Delete
#
[474] Fix | Delete
# [<tt>:indentation</tt>] Number of space characters used to indent.
[475] Fix | Delete
# Acceptable value should be in <tt>0..9</tt> range,
[476] Fix | Delete
# otherwise option is ignored.
[477] Fix | Delete
#
[478] Fix | Delete
# Default: <tt>2</tt>.
[479] Fix | Delete
# [<tt>:line_width</tt>] Max character to wrap line at.
[480] Fix | Delete
#
[481] Fix | Delete
# Default: <tt>0</tt> (meaning "wrap at 81").
[482] Fix | Delete
# [<tt>:canonical</tt>] Write "canonical" YAML form (very verbose, yet
[483] Fix | Delete
# strictly formal).
[484] Fix | Delete
#
[485] Fix | Delete
# Default: <tt>false</tt>.
[486] Fix | Delete
# [<tt>:header</tt>] Write <tt>%YAML [version]</tt> at the beginning of document.
[487] Fix | Delete
#
[488] Fix | Delete
# Default: <tt>false</tt>.
[489] Fix | Delete
#
[490] Fix | Delete
# Example:
[491] Fix | Delete
#
[492] Fix | Delete
# # Dump an array, get back a YAML string
[493] Fix | Delete
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
[494] Fix | Delete
#
[495] Fix | Delete
# # Dump an array to an IO object
[496] Fix | Delete
# Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
[497] Fix | Delete
#
[498] Fix | Delete
# # Dump an array with indentation set
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function