YAML::Any
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 <<-YAML
---
foo:
bar:
baz:
- qux
- fox
YAML
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. There are also nil-able variants (#as_i?, #as_s?, ...),
which return nil when the underlying value type won't match.
Constructors
Instance methods
Assumes the underlying value is an Array or Hash
and returns the element at the given index_or_key.
Raises if the underlying value is not an Array nor a Hash.
Assumes the underlying value is an Array or Hash and returns the element
at the given index_or_key, or nil if out of bounds or the key is missing.
Raises if the underlying value is not an Array nor a Hash.
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.
Returns nil otherwise.
Checks that the underlying value is Bytes, and returns its value.
Returns nil otherwise.
Checks that the underlying value is Float (or Int), and returns its value.
Raises otherwise.
Checks that the underlying value is Float (or Int), and returns its value as an Float32.
Raises otherwise.
Checks that the underlying value is Float (or Int), and returns its value as an Float32.
Returns nil otherwise.
Checks that the underlying value is Float (or Int), and returns its value.
Returns nil otherwise.
Checks that the underlying value is Int64, and returns its value as Int32.
Raises otherwise.
Checks that the underlying value is Int64, and returns its value.
Returns nil otherwise.
Checks that the underlying value is Int64, and returns its value as Int32.
Returns nil 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.
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.
Appends this struct's name and instance variables names and values to the given IO.
struct Point
def initialize(@x : Int32, @y : Int32)
end
end
p1 = Point.new 1, 2
p1.to_s # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"
Assumes the underlying value is an Array or Hash and returns its size.
Raises if the underlying value is not an Array or Hash.
Forwards to_json_object_key to raw if it responds to that method,
raises JSON::Error otherwise.