Class: Hand

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rubycards/hand.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards = []) ⇒ Hand

initialize with an array of cards



8
9
10
11
12
13
# File 'lib/rubycards/hand.rb', line 8

def initialize(cards = [])
  @cards = []
  cards.each do |card|
    self << card
  end
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



5
6
7
# File 'lib/rubycards/hand.rb', line 5

def cards
  @cards
end

Instance Method Details

#[](n) ⇒ Object

indexing



24
25
26
# File 'lib/rubycards/hand.rb', line 24

def [](n)
  @cards[n]
end

#add(card) ⇒ Object Also known as: <<

add a card if it is valid



16
17
18
# File 'lib/rubycards/hand.rb', line 16

def add(card)
  @cards << card if card.instance_of? Card
end

#draw(deck, n = 1) ⇒ Object

draw n cards from a deck



34
35
36
37
38
# File 'lib/rubycards/hand.rb', line 34

def draw(deck, n = 1)
  n.times do
    @cards << deck.draw unless deck.empty?
  end
end

#each(&block) ⇒ Object

Enumerable#each



41
42
43
44
45
46
47
48
49
# File 'lib/rubycards/hand.rb', line 41

def each(&block)
  @cards.each do |card|
    if block_given?
      block.call card
    else
      yield card
    end
  end
end

#sort!Object

sorting



29
30
31
# File 'lib/rubycards/hand.rb', line 29

def sort!
  @cards.sort!
end

#to_sObject

displaying the card icons using extensions/string.rb for String#next



53
54
55
# File 'lib/rubycards/hand.rb', line 53

def to_s
  @cards.map(&:to_s).inject(:next)
end