Class: Farce::Abstract::Collection Abstract

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Internal::Copyable, Internal::Inspect
Defined in:
lib/farce/abstract/collection.rb

Overview

This class is abstract.

Superclass for a collection of elements.

Subclasses define traversal, comparison, and insertion rules. They may accept additional optional parameters for the methods below.

Notable subclasses include Vector, Set, and SortedSet.

Direct Known Subclasses

Set, Vector

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Internal::Copyable

#duplicable?

Class Method Details

.[](*elements, **options) ⇒ Farce::Abstract::Collection

Construct a collection from the arguments.

Parameters:

  • elements (Array<BasicObject>) —

    The initial elements.

  • **options (Hash) —

    Additional options passed to the constructor.

Returns:



95
# File 'lib/farce/abstract/collection.rb', line 95

def self.[](*elements, **) = new(elements, **)

Instance Method Details

#<<(element) ⇒ self

This method is abstract.

Add an element using the collection's insertion rules and return self.

Parameters:

  • element (BasicObject) —

    The element to add.

Returns:

  • (self) —

    The collection.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#clear ⇒ self

This method is abstract.

Remove all elements and return self.

Returns:

  • (self) —

    The collection.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#compare_by_identity? ⇒ Boolean

This method is abstract.

Return whether elements are compared by object identity.

Returns:

  • (Boolean) —

    Whether identity comparison is enabled.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#count(item = UNDEFINED) {|element| ... } ⇒ Integer

Count all elements, matching elements, or elements accepted by a block. Without an argument or block, read the size without traversing elements.

Parameters:

  • item (BasicObject) (defaults to: UNDEFINED) —

    The value to count. Omit to count all elements or use the block.

Yields:

  • (element) —

    Select elements to count when item is omitted.

Yield Parameters:

  • element (BasicObject) —

    The current element.

Yield Returns:

  • (BasicObject) —

    A truthy value to count the element.

Returns:

  • (Integer) —

    The number of matching elements.



112
113
114
115
116
117
118
119
120
121
# File 'lib/farce/abstract/collection.rb', line 112

def count(item = UNDEFINED)
  return size if item.equal?(UNDEFINED) && !block_given?

  if item.equal?(UNDEFINED)
    super() { yield it }
  else
    warn("given block not used") if block_given?
    super(item, &nil)
  end
end

#each {|element| ... } ⇒ self, Enumerator

This method is abstract.

Iterate over the elements currently present.

Yields:

  • (element) —

    Visit each element. Returns an Enumerator without a block.

Yield Parameters:

  • element (BasicObject) —

    The current element.

Yield Returns:

  • (void) —

    The result is ignored.

Returns:

  • (self, Enumerator) —

    Self, or a sized Enumerator without a block.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#empty? ⇒ Boolean

Return whether no elements are present.

Returns:

  • (Boolean) —

    Whether the collection is empty.



103
# File 'lib/farce/abstract/collection.rb', line 103

def empty? = size.zero?

#filter {|element| ... } ⇒ Farce::Abstract::Collection, Enumerator

This method is abstract.

Return elements accepted by the block, as with #select.

Yields:

  • (element) —

    Test each element. Returns an Enumerator without a block.

Yield Parameters:

  • element (BasicObject) —

    The current element.

Yield Returns:

  • (BasicObject) —

    A truthy value to retain the element.

Returns:

See Also:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#include?(element) ⇒ Boolean

This method is abstract.

Return whether an element is present using the collection's membership rules.

Parameters:

  • element (BasicObject) —

    The element to look up.

Returns:

  • (Boolean) —

    Whether the element is present.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#join(separator = nil) ⇒ String

Join the elements through Array#join.

Parameters:

  • separator (String, nil) (defaults to: nil) —

    The separator. Nil uses Array's default separator.

Returns:

  • (String) —

    The joined elements.



126
# File 'lib/farce/abstract/collection.rb', line 126

def join(separator = nil) = to_a.join(separator)

#length ⇒ Integer

Return the number of elements currently present.

Returns:

  • (Integer) —

    The collection's size.



99
# File 'lib/farce/abstract/collection.rb', line 99

def length = size

#member?(element) ⇒ Boolean

This method is abstract.

Return whether an element is present, as with #include?.

Parameters:

  • element (BasicObject) —

    The element to look up.

Returns:

  • (Boolean) —

    Whether the element is present.

See Also:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#reject {|element| ... } ⇒ Farce::Abstract::Collection, Enumerator

This method is abstract.

Return a collection of the same kind excluding elements accepted by the block.

Yields:

  • (element) —

    Test each element. Returns an Enumerator without a block.

Yield Parameters:

  • element (BasicObject) —

    The current element.

Yield Returns:

  • (BasicObject) —

    A truthy value to exclude the element.

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#select {|element| ... } ⇒ Farce::Abstract::Collection, Enumerator

This method is abstract.

Return a collection of the same kind containing elements accepted by the block.

Yields:

  • (element) —

    Test each element. Returns an Enumerator without a block.

Yield Parameters:

  • element (BasicObject) —

    The current element.

Yield Returns:

  • (BasicObject) —

    A truthy value to retain the element.

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#size ⇒ Integer

This method is abstract.

Return the number of elements currently present.

Returns:

  • (Integer) —

    The number of elements.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#to_a ⇒ Array<BasicObject>

This method is abstract.

Return the observed elements in a new Array.

Returns:

  • (Array<BasicObject>) —

    The observed elements.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/farce/abstract/collection.rb', line 85

class Collection
  include Enumerable
  include Internal::Copyable
  include Internal::Inspect

  # @overload [](*elements, **options)
  #   Construct a collection from the arguments.
  #   @param elements [Array<BasicObject>] The initial elements.
  #   @param **options [Hash] Additional options passed to the constructor.
  #   @return [Farce::Abstract::Collection] A new instance of the receiving class.
  def self.[](*elements, **) = new(elements, **)

  # Return the number of elements currently present.
  # @return [Integer] The collection's size.
  def length = size

  # Return whether no elements are present.
  # @return [Boolean] Whether the collection is empty.
  def empty? = size.zero?

  # Count all elements, matching elements, or elements accepted by a block.
  # Without an argument or block, read the size without traversing elements.
  # @param item [BasicObject] The value to count. Omit to count all elements or use the block.
  # @yield [element] Select elements to count when item is omitted.
  # @yieldparam element [BasicObject] The current element.
  # @yieldreturn [BasicObject] A truthy value to count the element.
  # @return [Integer] The number of matching elements.
  def count(item = UNDEFINED)
    return size if item.equal?(UNDEFINED) && !block_given?

    if item.equal?(UNDEFINED)
      super() { yield it }
    else
      warn("given block not used") if block_given?
      super(item, &nil)
    end
  end

  # Join the elements through Array#join.
  # @param separator [String, nil] The separator. Nil uses Array's default separator.
  # @return [String] The joined elements.
  def join(separator = nil) = to_a.join(separator)

  # @return [String] The class name and observed elements, suitable for debugging.
  def to_s = inspect

  # @api private
  def inspect_with(inspector)
    super do
      yield if block_given?
      inspector.breakable
      inspector.group("[", "]") do
        inspector.breakable ""
        inspector.seplist(self, nil, :each_for_inspect) do |value|
          inspector.group { inspect_value(inspector, value) }
        end
      end
    end
  end

  private

  def each_for_inspect(&)           = each(&)
  def inspect_value(inspector, ...) = inspector.object(...)
end

#to_s ⇒ String

Returns The class name and observed elements, suitable for debugging.

Returns:

  • (String) —

    The class name and observed elements, suitable for debugging.



129
# File 'lib/farce/abstract/collection.rb', line 129

def to_s = inspect