Module: HandIteration

Included in:
Hand
Defined in:
tomes/components/deck_builder/hand_iteration.rb

Overview

Provides some methods for iterating over a hand.

Instance Method Summary collapse

Instance Method Details

#eachVoid Also known as: each_with_index

Iterates through #cards and calls the block on each Card.

Returns:

  • (Void)


7
8
9
10
11
12
13
14
15
# File 'tomes/components/deck_builder/hand_iteration.rb', line 7

def each
  index = 0
  iteration_limit = cards.length

  until index >= iteration_limit
    yield cards[index], index
    index += 1
  end
end

#index(search_card, &block) ⇒ Array<Integers>

Calls ‘#index` on #cards.

Returns:

  • (Array<Integers>)


38
39
40
# File 'tomes/components/deck_builder/hand_iteration.rb', line 38

def index(search_card, &block)
  cards.index(search_card, &block)
end

#map(&block) ⇒ Array

Calls #map on #cards.

Returns:

  • (Array)


20
21
22
# File 'tomes/components/deck_builder/hand_iteration.rb', line 20

def map(&block)
  cards.map(&block)
end

#selectArray<Cards>

Returns an array of cards for which the block returns a truthy value.

Returns:

  • (Array<Cards>)


26
27
28
29
30
31
32
33
34
# File 'tomes/components/deck_builder/hand_iteration.rb', line 26

def select
  selection = []

  each do |card|
    selection << card if yield card
  end

  selection
end