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