struct

YAML::Any

Inherits Struct / Value / Object

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

new(raw : Type)

Creates a YAML::Any that wraps the given Type.

Source
new(raw : Int)

Creates a YAML::Any that wraps the given Type.

Source
new(raw : Float)

Creates a YAML::Any that wraps the given Type.

Source

Instance methods

==(other : YAML::Any)

Returns true if both self and other's raw object are equal.

Source
==(other)

Returns true if the raw object is equal to other.

Source
[](index_or_key) : YAML::Any

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.

Source
[]?(index_or_key) : YAML::Any | Nil

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.

Source
as_a

Checks that the underlying value is Array, and returns its value. Raises otherwise.

Source
as_a?

Checks that the underlying value is Array, and returns its value. Returns nil otherwise.

Source
as_bool

Checks that the underlying value is Bool, and returns its value. Raises otherwise.

Source
as_bool?

Checks that the underlying value is Bool, and returns its value. Returns nil otherwise.

Source
as_bytes

Checks that the underlying value is Bytes, and returns its value. Raises otherwise.

Source
as_bytes?

Checks that the underlying value is Bytes, and returns its value. Returns nil otherwise.

Source
as_f

Checks that the underlying value is Float (or Int), and returns its value. Raises otherwise.

Source
as_f32

Checks that the underlying value is Float (or Int), and returns its value as an Float32. Raises otherwise.

Source
as_f32?

Checks that the underlying value is Float (or Int), and returns its value as an Float32. Returns nil otherwise.

Source
as_f?

Checks that the underlying value is Float (or Int), and returns its value. Returns nil otherwise.

Source
as_h

Checks that the underlying value is Hash, and returns its value. Raises otherwise.

Source
as_h?

Checks that the underlying value is Hash, and returns its value. Returns nil otherwise.

Source
as_i

Checks that the underlying value is Int64, and returns its value as Int32. Raises otherwise.

Source
as_i64

Checks that the underlying value is Int64, and returns its value. Raises otherwise.

Source
as_i64?

Checks that the underlying value is Int64, and returns its value. Returns nil otherwise.

Source
as_i?

Checks that the underlying value is Int64, and returns its value as Int32. Returns nil otherwise.

Source
as_nil

Checks that the underlying value is Nil, and returns nil. Raises otherwise.

Source
as_s

Checks that the underlying value is String, and returns its value. Raises otherwise.

Source
as_s?

Checks that the underlying value is String, and returns its value. Returns nil otherwise.

Source
as_time

Checks that the underlying value is Time, and returns its value. Raises otherwise.

Source
as_time?

Checks that the underlying value is Time, and returns its value. Returns nil otherwise.

Source
clone

Returns a new YAML::Any instance with the raw value cloneed.

Source
dig(index_or_key, *subkeys) : YAML::Any

Traverses the depth of a structure and returns the value, otherwise raises.

Source
dig?(index_or_key, *subkeys) : YAML::Any | Nil

Traverses the depth of a structure and returns the value. Returns nil if not found.

Source
dup

Returns a new YAML::Any instance with the raw value duped.

Source
hash(hasher)

See Object#hash(hasher)

Source
inspect(io : IO) : Nil

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)"
Source
raw

Returns the raw underlying value, a Type.

Source
size

Assumes the underlying value is an Array or Hash and returns its size.

Raises if the underlying value is not an Array or Hash.

Source
to_json(builder : JSON::Builder) : Nil
Source
to_json_object_key

Forwards to_json_object_key to raw if it responds to that method, raises JSON::Error otherwise.

Source
to_s(io : IO) : Nil

Same as #inspect(io).

Source

Nested types