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" (String)
language[:year]  # => 2011      (Int32)
language[:other] # compile time errorThe 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:
named_tuple.crjson/to_json.cr
yaml/to_yaml.cr
Class Method Summary
Instance Method Summary
- 
        #==(other : NamedTuple)
        
          Returns trueif this tuple has the same keys as other, and values for each key are the same in self and other.
- 
        #==(other : self)
        
          Returns trueif this tuple has the same keys as other, and values for each key are the same in self and other.
- 
        #[](key : Symbol)
        
          Returns the value for the given key, if there's such key, otherwise raises KeyError.
- 
        #[]?(key : Symbol)
        
          Returns the value for the given key, if there's such key, otherwise returns nil.
- 
        #clone
        
          Returns a named tuple with the same keys but with cloned values, using the #clonemethod.
- 
        #dup
        
          Returns self. 
- 
        #each(&block)
        
          Yields each key and value in this named tuple. 
- 
        #each_key(&block)
        
          Yields each key in this named tuple. 
- 
        #each_value(&block)
        
          Yields each value in this named tuple. 
- 
        #each_with_index(offset = 0, &block)
        
          Yields each key and value, together with an index starting at offset, in this named tuple. 
- 
        #empty?
        
          Returns trueif this named tuple is empty.
- 
        #fetch(key : Symbol, default_value)
        
          Returns the value for the given key, if there's such key, otherwise returns default_value. 
- 
        #fetch(key : Symbol, &block)
        
          Returns the value for the given key, if there's such key, otherwise the value returned by the block. 
- 
        #has_key?(key : Symbol) : Bool
        
          Returns trueif this named tuple has the given key,falseotherwise.
- 
        #hash
        
          Returns a hash value based on this name tuple's size, keys and values. 
- 
        #inspect
        
          Same as #to_s.
- 
        #keys
        
          Returns a Tupleof symbols with the keys in this named tuple.
- 
        #map(&block)
        
          Returns an Arraypopulated with the results of each iteration in the given block, which is given each key and value in this named tuple.
- 
        #size
        
          Returns the number of elements in this named tuple. 
- 
        #to_a
        
          Returns a new Arrayof tuples populated with each key-value pair.
- 
        #to_h
        
          Returns a Hashwith the keys and values in this named tuple.
- #to_json(io : IO)
- 
        #to_s(io)
        
          Appends a string representation of this named tuple to the given IO.
- #to_yaml(yaml : YAML::Generator)
- 
        #values
        
          Returns a Tuplewith the values in this named tuple.
Instance methods inherited from struct Value
  
    
      ==(other)
    ==
  
    
  
    
  Instance methods inherited from class Object
  
    
      !=(other)
    !=, 
  
    
      !~(other)
    !~, 
  
    
      ==(other)
    ==, 
  
    
      ===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO) inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try
Class methods inherited from class Object
  
    
      ==(other : Class)
    ==, 
  
    
      ===(other)
    ===, 
  
    
      cast(other) : self
    cast, 
  
    
      from_json(string_or_io) : self
    from_json, 
  
    
      from_yaml(string : String) : self
    from_yaml, 
  
    
      hash
    hash, 
  
    
      inspect(io)
    inspect, 
  
    
      name : String
    name, 
  
    
      to_s(io)
    to_s, 
  
    
      |(other : U.class)
    |
  
  
Class Method Detail
Instance Method Detail
Returns true if this tuple has the same keys as other, and values
for each key are the same in self and other.
tuple1 = {name: "Crystal", year: 2011}
tuple2 = {year: 2011, name: "Crystal"}
tuple3 = {name: "Crystal", year: 2012}
tuple4 = {name: "Crystal", year: 2011.0}
tuple1 == tuple2 # => true
tuple1 == tuple3 # => false
tuple1 == tuple4 # => trueReturns true if this tuple has the same keys as other, and values
for each key are the same in self and other.
tuple1 = {name: "Crystal", year: 2011}
tuple2 = {year: 2011, name: "Crystal"}
tuple3 = {name: "Crystal", year: 2012}
tuple4 = {name: "Crystal", year: 2011.0}
tuple1 == tuple2 # => true
tuple1 == tuple3 # => false
tuple1 == tuple4 # => trueReturns the value for the given key, if there's such key, otherwise raises KeyError.
tuple = {name: "Crystal", year: 2011}
key = :name
tuple[key] # => "Crystal"
key = :other
tuple[key] # # => KeyErrorReturns the value for the given key, if there's such key, otherwise returns nil.
tuple = {name: "Crystal", year: 2011}
key = :name
tuple[key]? # => "Crystal"
key = :other
tuple[key]? # => nilReturns a named tuple with the same keys but with cloned values, using the #clone method.
Yields each key and value in this named tuple.
tuple = {name: "Crystal", year: 2011}
tuple.each do |key, value|
  puts "#{key} = #{value}"
endOutput:
name = Crystal
year = 2011Yields each key in this named tuple.
tuple = {name: "Crystal", year: 2011}
tuple.each_key do |key|
  puts key
endOutput:
name
yearYields each value in this named tuple.
tuple = {name: "Crystal", year: 2011}
tuple.each_value do |value|
  puts value
endOutput:
Crystal
2011Yields each key and value, together with an index starting at offset, in this named tuple.
tuple = {name: "Crystal", year: 2011}
tuple.each_with_index do |key, value, i|
  puts "#{i + 1}) #{key} = #{value}"
endOutput:
1) name = Crystal
2) year = 2011Returns true if this named tuple is empty.
tuple = {name: "Crystal", year: 2011}
tuple.empty? # => falseReturns the value for the given key, if there's such key, otherwise returns default_value.
tuple = {name: "Crystal", year: 2011}
tuple.fetch(:name, "Unknown") # => "Crystal"
tuple.fetch(:other, 0)        # => 0Returns the value for the given key, if there's such key, otherwise the value returned by the block.
tuple = {name: "Crystal", year: 2011}
tuple.fetch(:name) { "Unknown" } # => "Crystal"
tuple.fetch(:other) { 0 }        # => 0Returns true if this named tuple has the given key, false otherwise.
tuple = {name: "Crystal", year: 2011}
tuple.has_key?(:name)  # => true
tuple.has_key?(:other) # => falseReturns a Tuple of symbols with the keys in this named tuple.
tuple = {name: "Crystal", year: 2011}
tuple.keys # => {:name, :year}Returns an Array populated with the results of each iteration in the given block,
which is given each key and value in this named tuple.
tuple = {name: "Crystal", year: 2011}
tuple.map { |k, v| "#{name}: #{year}" } # => ["name: Crystal", "year: 2011"]Returns the number of elements in this named tuple.
tuple = {name: "Crystal", year: 2011}
tuple.size # => 2Returns a new Array of tuples populated with each key-value pair.
tuple = {name: "Crystal", year: 2011}
tuple.to_a # => [{:name, "Crystal"}, {:year, 2011}]Returns a Hash with the keys and values in this named tuple.
tuple = {name: "Crystal", year: 2011}
tuple.to_h # => {:name => "Crystal", :year => 2011}Appends a string representation of this named tuple to the given IO.
tuple = {name: "Crystal", year: 2011}
tuple.to_s # => %({name: "Crystal", year: 2011})