Class: Rubyzen::Collections::BaseCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/rubyzen/collections/base_collection.rb

Overview

Base collection class for all Rubyzen collections. Extends Array and replaces select/reject with a single filter method that preserves the collection subclass type.

Examples:

Filtering a collection with a block

controllers.filter { |c| c.name.end_with?('Controller') }

Instance Method Summary collapse

Instance Method Details

#filter {|element| ... } ⇒ BaseCollection, Enumerator

Filters elements by the given block, returning a new collection of the same type.

Yields:

  • (element)

    block that returns truthy to keep the element

Returns:

  • (BaseCollection)

    a new collection containing only matching elements

  • (Enumerator)

    if no block is given



18
19
20
21
22
23
24
25
26
27
# File 'lib/rubyzen/collections/base_collection.rb', line 18

def filter
  return enum_for(:filter) unless block_given?

  result = self.class.new
  each do |elem|
    result << elem if yield(elem)
  end

  result
end