Class: CowProxy::Array

Inherits:
WrapClass
  • Object
show all
Includes:
Indexable, Enumerable
Defined in:
lib/cow_proxy/array.rb

Overview

Wrapper class for Array

Instance Method Summary collapse

Methods included from Indexable

#[], #dig, #initialize

Instance Method Details

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

Calls the given block once for each element in self, passing wrapped element as a parameter.

Yields:

  • (item)

    Gives each element in self to the block

Yield Parameters:

  • item

    Wrapped item in self

Returns:

  • (CowProxy::Array)

    self if block given

  • (Enumerator)

    if no block given



15
16
17
18
19
20
21
# File 'lib/cow_proxy/array.rb', line 15

def each
  return enum_for(:each) unless block_given?
  __getobj__.each.with_index do |_, i|
    yield self[i]
  end
  self
end

#map! {|item| ... } ⇒ CowProxy::Array, Enumerator Also known as: collect!

Invokes the given block once for each element of self, replacing the element with the value returned by the block.

Yields:

  • (item)

    Gives each element in self to the block

Yield Parameters:

  • item

    Wrapped item in self

Yield Returns:

  • item to replace

Returns:

  • (CowProxy::Array)

    self if block given

  • (Enumerator)

    if no block given



31
32
33
34
35
36
37
# File 'lib/cow_proxy/array.rb', line 31

def map!
  __copy_on_write__
  return enum_for(:map!) unless block_given?
  __getobj__.each.with_index do |_, i|
    self[i] = yield(self[i])
  end
end

#to_aryArray

Used for concatenating into another Array needs to return unwrapped Array

Returns:

  • (Array)

    wrapped object



44
45
46
# File 'lib/cow_proxy/array.rb', line 44

def to_ary
  __getobj__
end