class RubyCards::Hand

Attributes

cards[R]

Public Class Methods

new(cards = []) click to toggle source

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

draw(deck, n = 1) click to toggle source

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
each(&block) click to toggle source

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
inspect() click to toggle source

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
sort!() click to toggle source

Sorts the hand and returns it

@return [Hand] The sorted hand

# File lib/rubycards/hand.rb, line 29
def sort!
  @cards.sort!
  self
end
sum() click to toggle source

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
to_s() click to toggle source

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