Module: Functional::AbstractStruct
Overview
An abstract base class for immutable struct classes.
Instance Attribute Summary (collapse)
-
- (Array) values
readonly
The values of all record fields in order, frozen.
Instance Method Summary (collapse)
-
- (Enumerable) each {|value| ... }
Yields the value of each record field in order.
-
- (Enumerable) each_pair {|field, value| ... }
Yields the name and value of each record field in order.
-
- (Boolean) eql?(other)
(also: #==)
Equality--Returns
true
ifother
has the same record subclass and has equal field values (according toObject#==
). -
- (Array) fields
A frozen array of all record fields.
-
- (String) inspect
(also: #to_s)
Describe the contents of this struct in a string.
-
- (Fixnum) length
(also: #size)
Returns the number of record fields.
-
- (Hash) to_h
Returns a Hash containing the names and values for the record’s fields.
Instance Attribute Details
- (Array) values (readonly)
Returns the values of all record fields in order, frozen
19 20 21 |
# File 'lib/functional/abstract_struct.rb', line 19 def values @values end |
Instance Method Details
- (Enumerable) each {|value| ... }
Yields the value of each record field in order. If no block is given an enumerator is returned.
27 28 29 30 31 32 |
# File 'lib/functional/abstract_struct.rb', line 27 def each return enum_for(:each) unless block_given? fields.each do |field| yield(self.send(field)) end end |
- (Enumerable) each_pair {|field, value| ... }
Yields the name and value of each record field in order. If no block is given an enumerator is returned.
41 42 43 44 45 46 |
# File 'lib/functional/abstract_struct.rb', line 41 def each_pair return enum_for(:each_pair) unless block_given? fields.each do |field| yield(field, self.send(field)) end end |
- (Boolean) eql?(other) Also known as: ==
Equality--Returns true
if other
has the same record subclass and has equal
field values (according to Object#==
).
53 54 55 |
# File 'lib/functional/abstract_struct.rb', line 53 def eql?(other) self.class == other.class && self.to_h == other.to_h end |
- (Array) fields
A frozen array of all record fields.
81 82 83 |
# File 'lib/functional/abstract_struct.rb', line 81 def fields self.class.fields end |
- (String) inspect Also known as: to_s
Describe the contents of this struct in a string. Will include the name of the record class, all fields, and all values.
64 65 66 67 |
# File 'lib/functional/abstract_struct.rb', line 64 def inspect state = to_h.to_s.gsub(/^{/, '').gsub(/}$/, '') "#<#{self.class.datatype} #{self.class} #{state}>" end |
- (Fixnum) length Also known as: size
Returns the number of record fields.
73 74 75 |
# File 'lib/functional/abstract_struct.rb', line 73 def length fields.length end |
- (Hash) to_h
Returns a Hash containing the names and values for the record’s fields.
88 89 90 |
# File 'lib/functional/abstract_struct.rb', line 88 def to_h @data end |