class RubyCards::Hand
Attributes
Public Class Methods
Initializes a hand of cards
@param cards [Array<Card>] A predetermined array of cards @return [Hand] The generated hand
# File lib/rubycards/hand.rb, line 19 def initialize(cards = []) @cards = [] cards.each do |card| self << card end end
Public Instance Methods
Draws n cards from a given deck and adds them to the Hand
@param deck [Deck] The deck to draw from @param n [Integer] The amount of cards to draw @return [Hand] The new hand
# File lib/rubycards/hand.rb, line 39 def draw(deck, n = 1) n.times do @cards << deck.draw unless deck.empty? end self end
Returns an enumator over the hand
@param block [Proc] The block to pass into the enumerator @return [Enumerator] An enumerator for the hand
# File lib/rubycards/hand.rb, line 57 def each(&block) @cards.each(&block) end
A shortened representation of the hand used for the console
@return [String] A concise string representation of the hand
# File lib/rubycards/hand.rb, line 71 def inspect "[ #{@cards.map(&:inspect).join ', '} ]" end
Sorts the hand and returns it
@return [Hand] The sorted hand
# File lib/rubycards/hand.rb, line 29 def sort! @cards.sort! self end
Returns the sum of the hand
@return [Integer] The combined weights of the cards in the hand
# File lib/rubycards/hand.rb, line 49 def sum this.cards.reduce { |memo, card| memo + card.to_i } end
Displays the hand using ASCII-art-style cards
@return [String] The ASCII representation of the hand
# File lib/rubycards/hand.rb, line 64 def to_s @cards.map(&:to_s).inject(:next) end