abstract struct Struct
Overview
Struct
is the base type of structs you create in your program.
It is set as a struct's superstruct when you don't specify one:
struct Foo # < Struct
end
Structs inherit from Value
so they are allocated on the stack and passed
by value. For this reason you should prefer using structs for immutable
data types and/or stateless wrappers of other types.
Mutable structs are still allowed, but code involving them must remember that passing a struct to a method actually passes a copy to it, so the method should return the modified struct:
struct Mutable
property value
def initialize(@value : Int32)
end
end
def change_bad(mutable)
mutable.value = 2
end
def change_good(mutable)
mutable.value = 2
mutable
end
mut = Mutable.new 1
change_bad(mut)
mut.value # => 1
mut = change_good(mut)
mut.value # => 2
The standard library provides a useful record
macro that allows you to
create immutable structs with some fields, similar to a Tuple
but using
names instead of indices.
Direct Known Subclasses
- Atomic(T)
- BitArray
- CallStack::RepeatedFrame
- Channel::ReceiveAction(C)
- Channel::SendAction(C, T)
- Char::Reader
- Colorize::Color256
- Colorize::ColorRGB
- Colorize::Object(T)
- Complex
- CSV::Builder::Row
- CSV::Row
- CSV::Token
- Debug::DWARF::Abbrev
- Debug::DWARF::Abbrev::Attribute
- Debug::DWARF::Info
- Debug::DWARF::LineNumbers
- Debug::DWARF::LineNumbers::Register
- Debug::DWARF::LineNumbers::Row
- Debug::DWARF::LineNumbers::Sequence
- Debug::DWARF::Strings
- Debug::ELF
- Debug::ELF::Ident
- Debug::ELF::SectionHeader
- Dir::Globber::ConstantDirectory
- Dir::Globber::ConstantEntry
- Dir::Globber::DirectoriesOnly
- Dir::Globber::DirectoryMatch
- Dir::Globber::EntryMatch
- Dir::Globber::RecursiveDirectories
- Dir::Globber::RootDirectory
- Enumerable::Chunk::Alone
- Enumerable::Chunk::Drop
- Event::DnsBase
- Event::DnsBase::GetAddrInfoRequest
- File::Info
- Float::Printer::CachedPowers::Power
- Float::Printer::DiyFP
- GC::Stats
- HTTP::ComputedContentTypeHeader
- HTTP::FormData::FileMetadata
- HTTP::FormData::Part
- HTTP::Headers
- HTTP::Params
- HTTP::StaticFileHandler::DirectoryListing
- HTTP::WebSocket::Protocol::PacketInfo
- IO::EncodingOptions
- JSON::Any
- JSON::Builder::ArrayState
- JSON::Builder::DocumentEndState
- JSON::Builder::DocumentStartState
- JSON::Builder::ObjectState
- JSON::Builder::StartState
- LLVM::ABI::ArgType
- LLVM::BasicBlock
- LLVM::BasicBlockCollection
- LLVM::DIBuilder
- LLVM::Function
- LLVM::FunctionCollection
- LLVM::FunctionPassManager::Runner
- LLVM::GlobalCollection
- LLVM::InstructionCollection
- LLVM::OperandBundleDef
- LLVM::ParameterCollection
- LLVM::PassRegistry
- LLVM::PhiTable
- LLVM::Target
- LLVM::TargetData
- LLVM::Type
- LLVM::Value
- Markdown::Parser::CodeFence
- Markdown::Parser::PrefixHeader
- Markdown::Parser::UnorderedList
- Pointer::Appender(T)
- Process::Tms
- Range(B, E)
- Reflect(X)
- Regex::MatchData
- Set(T)
- Slice(T)
- Socket::Address
- Socket::Addrinfo
- String::Formatter::Flags
- Time
- Time::Format
- Time::Location::Zone
- Time::MonthSpan
- Time::Span
- UUID
- VaList
- XML::Attributes
- XML::Builder
- XML::Namespace
- XML::Node
- XML::NodeSet
- XML::Reader
- XML::XPathContext
- YAML::Any
Defined in:
struct.crInstance Method Summary
-
#==(other) : Bool
Returns
true
if this struct is equal to other. - #hash(hasher)
-
#inspect(io : IO) : Nil
Appends this struct's name and instance variables names and values to the given IO.
- #pretty_print(pp) : Nil
-
#to_s(io)
Same as
#inspect(io)
.
Instance methods inherited from struct Value
==(other : JSON::Any)==(other : YAML::Any)
==(other) ==, dup dup
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other : JSON::Any)===(other : YAML::Any)
===(other) ===, =~(other) =~, class class, dup dup, hash(hasher)
hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as
Constructor methods inherited from class Object
from_json(string_or_io, root : String) : selffrom_json(string_or_io) : self from_json, from_yaml(string_or_io : String | IO) : self from_yaml
Instance Method Detail
Returns true
if this struct is equal to other.
Both structs's instance vars are compared to each other. Thus, two structs are considered equal if each of their instance variables are equal. Subclasses should override this method to provide specific equality semantics.
struct Point
def initialize(@x : Int32, @y : Int32)
end
end
p1 = Point.new 1, 2
p2 = Point.new 1, 2
p3 = Point.new 3, 4
p1 == p2 # => true
p1 == p3 # => false
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)"