struct NamedTuple(**T)

Overview

A named tuple is a fixed-size, immutable, stack-allocated mapping of a fixed set of keys to values.

You can think of a NamedTuple as an immutable Hash whose keys (which are of type Symbol), and the types for each key, are known at compile time.

A named tuple can be created with a named tuple literal:

language = {name: "Crystal", year: 2011} # NamedTuple(name: String, year: Int32)

language[:name]  # => "Crystal"
language[:year]  # => 2011
language[:other] # compile time error

The compiler knows what types are in each key, so when indexing a named tuple with a symbol literal the compiler will return the value for that key and with the expected type, like in the above snippet. Indexing with a symbol literal for which there's no key will give a compile-time error.

Indexing with a symbol that is only known at runtime will return a value whose type is the union of all the types in the named tuple, and might raise KeyError.

Defined in:

crystal_on_steroids/named_tuple.cr
crystal_on_steroids/to_query.cr

Instance Method Summary

Instance methods inherited from class Object

in?(another_object) in?, presence presence, presence_in(another_object) presence_in, present? present?, to_param to_param, to_query(namespace)
to_query
to_query

Class methods inherited from class Object

❨╯°□°❩╯︵┻━┻ ❨╯°□°❩╯︵┻━┻

Instance Method Detail

def blank? #

Returns true if this named tuple is empty.

tuple = {name: "Crystal", year: 2011}
tuple.blank? # => false

[View source]
def to_query(namespace = nil) #
Description copied from class Object

Converts an object into a string suitable for use as a URL query string, using the given namespace as the param name.


[View source]