Method: Psych.unsafe_load
- Defined in:
- lib/psych.rb
.unsafe_load(yaml, filename: nil, fallback: false, symbolize_names: false, freeze: false, strict_integer: false) ⇒ Object
Load yaml
in to a Ruby data structure. If multiple documents are provided, the object contained in the first document will be returned. filename
will be used in the exception message if any exception is raised while parsing. If yaml
is empty, it returns the specified fallback
return value, which defaults to false
.
Raises a Psych::SyntaxError when a YAML syntax error is detected.
Example:
Psych.unsafe_load("--- a") # => 'a'
Psych.unsafe_load("---\n - a\n - b") # => ['a', 'b']
begin
Psych.unsafe_load("--- `", filename: "file.txt")
rescue Psych::SyntaxError => ex
ex.file # => 'file.txt'
ex. # => "(file.txt): found character that cannot start any token"
end
When the optional symbolize_names
keyword argument is set to a true value, returns symbols for keys in Hash objects (default: strings).
Psych.unsafe_load("---\n foo: bar") # => {"foo"=>"bar"}
Psych.unsafe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
Raises a TypeError when ‘yaml` parameter is NilClass
NOTE: This method *should not* be used to parse untrusted documents, such as YAML documents that are supplied via user input. Instead, please use the load method or the safe_load method.
273 274 275 276 277 |
# File 'lib/psych.rb', line 273 def self.unsafe_load yaml, filename: nil, fallback: false, symbolize_names: false, freeze: false, strict_integer: false result = parse(yaml, filename: filename) return fallback unless result result.to_ruby(symbolize_names: symbolize_names, freeze: freeze, strict_integer: strict_integer) end |