YAML::Any
is a convenient wrapper around all possible YAML core types (YAML::Any::Type
) and can be used for traversing dynamic or unknown YAML structures.
require "yaml" data = YAML.parse <<-END --- foo: bar: baz: - qux - fox END data["foo"]["bar"]["baz"][0].as_s # => "qux" data["foo"]["bar"]["baz"].as_a # => ["qux", "fox"]
Note that methods used to traverse a YAML structure, #[]
, #[]?
and #each
, always return a YAML::Any
to allow further traversal. To convert them to String
, Array
, etc., use the as_
methods, such as #as_s
, #as_a
, which perform a type check against the raw underlying value. This means that invoking #as_s
when the underlying value is not a String
will raise: the value won't automatically be converted (parsed) to a String
.
Returns true
if the raw object is equal to other.
Returns true
if both self
and other's raw object are equal.
Checks that the underlying value is Array
, and returns its value.
Checks that the underlying value is Array
, and returns its value.
Checks that the underlying value is Bool
, and returns its value.
Checks that the underlying value is Bool
, and returns its value.
Checks that the underlying value is Bytes
, and returns its value.
Checks that the underlying value is Bytes
, and returns its value.
Checks that the underlying value is Float64
, and returns its value.
Checks that the underlying value is Float64
, and returns its value.
Checks that the underlying value is Hash
, and returns its value.
Checks that the underlying value is Hash
, and returns its value.
Checks that the underlying value is Int64
, and returns its value.
Checks that the underlying value is Int64
, and returns its value.
Checks that the underlying value is Nil
, and returns nil
.
Checks that the underlying value is String
, and returns its value.
Checks that the underlying value is String
, and returns its value.
Checks that the underlying value is Time
, and returns its value.
Checks that the underlying value is Time
, and returns its value.
Traverses the depth of a structure and returns the value, otherwise raises.
Traverses the depth of a structure and returns the value.
Returns the raw underlying value, a Type
.
Forwards #to_json_object_key
to #raw
if it responds to that method, raises JSON::Error
otherwise.
Struct
Value
Object
Object
Returns true
if the raw object is equal to other.
Checks that the underlying value is Array
, and returns its value. Raises otherwise.
Checks that the underlying value is Array
, and returns its value. Returns nil
otherwise.
Checks that the underlying value is Bool
, and returns its value. Raises otherwise.
Checks that the underlying value is Bool
, and returns its value. Returns nil
otherwise.
Checks that the underlying value is Bytes
, and returns its value. Raises otherwise.
Checks that the underlying value is Bytes
, and returns its value. Returns nil
otherwise.
Checks that the underlying value is Float64
, and returns its value. Raises otherwise.
Checks that the underlying value is Float64
, and returns its value. Returns nil
otherwise.
Checks that the underlying value is Hash
, and returns its value. Raises otherwise.
Checks that the underlying value is Hash
, and returns its value. Returns nil
otherwise.
Checks that the underlying value is Int64
, and returns its value. Raises otherwise.
Checks that the underlying value is Int64
, and returns its value. Returns nil
otherwise.
Checks that the underlying value is String
, and returns its value. Raises otherwise.
Checks that the underlying value is String
, and returns its value. Returns nil
otherwise.
Checks that the underlying value is Time
, and returns its value. Raises otherwise.
Checks that the underlying value is Time
, and returns its value. Returns nil
otherwise.
Traverses the depth of a structure and returns the value, otherwise raises.
Traverses the depth of a structure and returns the value. Returns nil
if not found.
Forwards #to_json_object_key
to #raw
if it responds to that method, raises JSON::Error
otherwise.
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/YAML/Any.html