Module: Functional::AbstractStruct
Overview
An abstract base class for immutable struct classes.
Instance Attribute Summary collapse
-
#values ⇒ Array
readonly
The values of all record fields in order, frozen.
Instance Method Summary collapse
-
#each {|value| ... } ⇒ Enumerable
Yields the value of each record field in order.
-
#each_pair {|field, value| ... } ⇒ Enumerable
Yields the name and value of each record field in order.
-
#eql?(other) ⇒ Booleab
(also: #==)
Equality–Returns
trueifotherhas the same record subclass and has equal field values (according to ‘Object#==`). -
#fields ⇒ Array
A frozen array of all record fields.
-
#inspect ⇒ String
(also: #to_s)
Describe the contents of this record in a string.
-
#length ⇒ Fixnum
(also: #size)
Returns the number of record fields.
-
#to_h ⇒ Hash
Returns a Hash containing the names and values for the record’s fields.
Instance Attribute Details
#values ⇒ Array (readonly)
Returns the values of all record fields in order, frozen.
17 18 19 |
# File 'lib/functional/abstract_struct.rb', line 17 def values @values end |
Instance Method Details
#each {|value| ... } ⇒ Enumerable
Yields the value of each record field in order. If no block is given an enumerator is returned.
25 26 27 28 29 30 |
# File 'lib/functional/abstract_struct.rb', line 25 def each return enum_for(:each) unless block_given? fields.each do |field| yield(self.send(field)) end end |
#each_pair {|field, value| ... } ⇒ Enumerable
Yields the name and value of each record field in order. If no block is given an enumerator is returned.
39 40 41 42 43 44 |
# File 'lib/functional/abstract_struct.rb', line 39 def each_pair return enum_for(:each_pair) unless block_given? fields.each do |field| yield(field, self.send(field)) end end |
#eql?(other) ⇒ Booleab Also known as: ==
Equality–Returns true if other has the same record subclass and has equal field values (according to ‘Object#==`).
51 52 53 |
# File 'lib/functional/abstract_struct.rb', line 51 def eql?(other) self.class == other.class && self.to_h == other.to_h end |
#fields ⇒ Array
A frozen array of all record fields.
79 80 81 |
# File 'lib/functional/abstract_struct.rb', line 79 def fields self.class.fields end |
#inspect ⇒ String Also known as: to_s
Describe the contents of this record in a string. Will include the name of the record class, all fields, and all values.
62 63 64 65 |
# File 'lib/functional/abstract_struct.rb', line 62 def inspect state = to_h.to_s.gsub(/^{/, '').gsub(/}$/, '') "#<#{self.class.datatype} #{self.class} #{state}>" end |
#length ⇒ Fixnum Also known as: size
Returns the number of record fields.
71 72 73 |
# File 'lib/functional/abstract_struct.rb', line 71 def length fields.length end |
#to_h ⇒ Hash
Returns a Hash containing the names and values for the record’s fields.
86 87 88 |
# File 'lib/functional/abstract_struct.rb', line 86 def to_h @data end |