Class: RubyCards::Hand
- Inherits:
-
Object
- Object
- RubyCards::Hand
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/rubycards/hand.rb
Instance Attribute Summary collapse
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
Instance Method Summary collapse
-
#draw(deck, n = 1) ⇒ Hand
Draws n cards from a given deck and adds them to the Hand.
-
#each(&block) ⇒ Enumerator
Returns an enumator over the hand.
-
#initialize(cards = []) ⇒ Hand
constructor
Initializes a hand of cards.
-
#inspect ⇒ String
A shortened representation of the hand used for the console.
-
#sort! ⇒ Hand
Sorts the hand and returns it.
-
#sum ⇒ Integer
Returns the sum of the hand.
-
#to_s ⇒ String
Displays the hand using ASCII-art-style cards.
Constructor Details
#initialize(cards = []) ⇒ Hand
Initializes a hand of cards
19 20 21 22 23 24 |
# File 'lib/rubycards/hand.rb', line 19 def initialize(cards = []) @cards = [] cards.each do |card| self << card end end |
Instance Attribute Details
#cards ⇒ Object (readonly)
Returns the value of attribute cards.
9 10 11 |
# File 'lib/rubycards/hand.rb', line 9 def cards @cards end |
Instance Method Details
#draw(deck, n = 1) ⇒ Hand
Draws n cards from a given deck and adds them to the Hand
39 40 41 42 43 44 |
# File 'lib/rubycards/hand.rb', line 39 def draw(deck, n = 1) n.times do @cards << deck.draw unless deck.empty? end self end |
#each(&block) ⇒ Enumerator
Returns an enumator over the hand
57 58 59 |
# File 'lib/rubycards/hand.rb', line 57 def each(&block) @cards.each(&block) end |
#inspect ⇒ String
A shortened representation of the hand used for the console
71 72 73 |
# File 'lib/rubycards/hand.rb', line 71 def inspect "[ #{@cards.map(&:inspect).join ', '} ]" end |
#sort! ⇒ Hand
Sorts the hand and returns it
29 30 31 32 |
# File 'lib/rubycards/hand.rb', line 29 def sort! @cards.sort! self end |
#sum ⇒ Integer
Returns the sum of the hand
49 50 51 |
# File 'lib/rubycards/hand.rb', line 49 def sum this.cards.reduce { |memo, card| memo + card.to_i } end |
#to_s ⇒ String
Displays the hand using ASCII-art-style cards
64 65 66 |
# File 'lib/rubycards/hand.rb', line 64 def to_s @cards.map(&:to_s).inject(:next) end |