Class: Farce::Abstract::Collection Abstract
- Inherits:
-
Object
- Object
- Farce::Abstract::Collection
- Includes:
- Enumerable, Internal::Copyable, Internal::Inspect
- Defined in:
- lib/farce/abstract/collection.rb
Overview
Class Method Summary collapse
-
.[](*elements, **options) ⇒ Farce::Abstract::Collection
Construct a collection from the arguments.
Instance Method Summary collapse
-
#<<(element) ⇒ self
abstract
Add an element using the collection's insertion rules and return self.
-
#clear ⇒ self
abstract
Remove all elements and return self.
-
#compare_by_identity? ⇒ Boolean
abstract
Return whether elements are compared by object identity.
-
#count(item = UNDEFINED) {|element| ... } ⇒ Integer
Count all elements, matching elements, or elements accepted by a block.
-
#each {|element| ... } ⇒ self, Enumerator
abstract
Iterate over the elements currently present.
-
#empty? ⇒ Boolean
Return whether no elements are present.
-
#filter {|element| ... } ⇒ Farce::Abstract::Collection, Enumerator
abstract
Return elements accepted by the block, as with #select.
-
#include?(element) ⇒ Boolean
abstract
Return whether an element is present using the collection's membership rules.
-
#join(separator = nil) ⇒ String
Join the elements through Array#join.
-
#length ⇒ Integer
Return the number of elements currently present.
-
#member?(element) ⇒ Boolean
abstract
Return whether an element is present, as with #include?.
-
#reject {|element| ... } ⇒ Farce::Abstract::Collection, Enumerator
abstract
Return a collection of the same kind excluding elements accepted by the block.
-
#select {|element| ... } ⇒ Farce::Abstract::Collection, Enumerator
abstract
Return a collection of the same kind containing elements accepted by the block.
-
#size ⇒ Integer
abstract
Return the number of elements currently present.
-
#to_a ⇒ Array<BasicObject>
abstract
Return the observed elements in a new Array.
-
#to_s ⇒ String
The class name and observed elements, suitable for debugging.
Methods included from Internal::Copyable
Class Method Details
.[](*elements, **options) ⇒ Farce::Abstract::Collection
95 |
# File 'lib/farce/abstract/collection.rb', line 95 def self.[](*elements, **) = new(elements, **) |
Instance Method Details
#<<(element) ⇒ self
Add an element using the collection's insertion rules and return self.
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
Remove all elements and return self.
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
Return whether elements are compared by object identity.
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.
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
Iterate over the elements currently 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 |
#empty? ⇒ Boolean
Return whether no elements are present.
103 |
# File 'lib/farce/abstract/collection.rb', line 103 def empty? = size.zero? |
#filter {|element| ... } ⇒ Farce::Abstract::Collection, Enumerator
Return elements accepted by the block, as with #select.
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
Return whether an element is present using the collection's membership rules.
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.
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.
99 |
# File 'lib/farce/abstract/collection.rb', line 99 def length = size |
#member?(element) ⇒ Boolean
Return whether an element is present, as with #include?.
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
Return a collection of the same kind excluding elements accepted by the 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 |
#select {|element| ... } ⇒ Farce::Abstract::Collection, Enumerator
Return a collection of the same kind containing elements accepted by the 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 |
#size ⇒ Integer
Return the number of elements currently 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 |
#to_a ⇒ Array<BasicObject>
Return the observed elements in a new Array.
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.
129 |
# File 'lib/farce/abstract/collection.rb', line 129 def to_s = inspect |