struct

Crig::OneOrMany(T)

Inherits Enumerable < Struct < Value < Object

Constructors

from_json(input : String) : self
Source
from_json_any(value : JSON::Any) : self
Source
many(items : Enumerable(T)) : self
Source
merge(items : Enumerable(self)) : self
Source
new(pull : JSON::PullParser) : self
Source
new(first : T, rest : Array(T) = [] of T)
Source
one(item : T) : self
Source

Class methods

from_iter_optional(items : Enumerable(T)) : self | Nil
Source

Instance methods

each

Must yield this collection's elements to the block.

Source
empty?

Returns true if self does not contain any element.

([] of Int32).empty? # => true
([1]).empty?         # => false
[nil, false].empty?  # => false
  • #present? returns the inverse.
Source
first

Returns the first element in the collection. Raises Enumerable::EmptyError if the collection is empty.

([1, 2, 3]).first   # => 1
([] of Int32).first # raises Enumerable::EmptyError
Source
first_mut
Source
first_ref
Source
insert(index : Int, item : T) : Nil
Source
into_iter
Source
is_empty

ameba:disable Naming/PredicateName

Source
iter
Source
iter_mut

Crystal cannot yield Rust-style mutable references, so this iterator returns the same underlying objects for reference types while preserving visit order.

Source
last
Source
last_mut
Source
last_ref
Source
map_one_or_many

Transforms each item to a new type, returning a new OneOrMany. Unlike Enumerable#map (which returns an Array), this preserves the OneOrMany guarantee.

Source
push(item : T) : Nil
Source
rest
Source
to_a

Returns an Array with all the elements in the collection.

(1..5).to_a # => [1, 2, 3, 4, 5]
Source
to_json(json : JSON::Builder) : Nil
Source

Nested types