Module: CowProxy::Enumerable

Defined in:
lib/cow_proxy/enumerable.rb

Overview

A mixin to add mutable methods for keep_if, delete_if, select! and reject! to wrap block params with CowProxy

Instance Method Summary collapse

Instance Method Details

#delete_if {|item| ... } ⇒ CowProxy::Array, Enumerator

Deletes every element of self for which block evaluates to true.

Yields:

  • (item)

    Gives each element in self to the block

Yield Parameters:

  • item

    Wrapped item in self

Yield Returns:

  • (Boolean)

    true if item must be deleted

Returns:

  • (CowProxy::Array)

    self if block given

  • (Enumerator)

    if no block given



37
38
39
# File 'lib/cow_proxy/enumerable.rb', line 37

def delete_if(&block)
  mutable_selector(:reject, &block)
end

#keep_if {|item| ... } ⇒ CowProxy::Array, Enumerator

Invokes the given block passing in successive elements from self, deleting elements for which the block returns a false value.

Yields:

  • (item)

    Gives each element in self to the block

Yield Parameters:

  • item

    Wrapped item in self

Yield Returns:

  • (Boolean)

    true if item must be kept

Returns:

  • (CowProxy::Array)

    self if block given

  • (Enumerator)

    if no block given



13
14
15
# File 'lib/cow_proxy/enumerable.rb', line 13

def keep_if(&block)
  mutable_selector(:select, &block)
end

#reject! {|item| ... } ⇒ CowProxy::Array, ...

Deletes every element of self for which block evaluates to true.

Yields:

  • (item)

    Gives each element in self to the block

Yield Parameters:

  • item

    Wrapped item in self

Yield Returns:

  • (Boolean)

    true if item must be deleted

Returns:

  • (CowProxy::Array)

    self if block given and changes were made

  • (nil)

    if block given and no changes were made

  • (Enumerator)

    if no block given



49
50
51
# File 'lib/cow_proxy/enumerable.rb', line 49

def reject!(&block)
  mutable_selector!(:delete_if, &block)
end

#select! {|item| ... } ⇒ CowProxy::Array, ...

Invokes the given block passing in successive elements from self, deleting elements for which the block returns a false value.

Yields:

  • (item)

    Gives each element in self to the block

Yield Parameters:

  • item

    Wrapped item in self

Yield Returns:

  • (Boolean)

    true if item must be kept

Returns:

  • (CowProxy::Array)

    self if block given and changes were made

  • (nil)

    if block given and no changes were made

  • (Enumerator)

    if no block given



26
27
28
# File 'lib/cow_proxy/enumerable.rb', line 26

def select!(&block)
  mutable_selector!(:keep_if, &block)
end