Sha256: 9bb11daec92e4fb2b153780ab285e20f5228d6847e1cd19634824aea8ccb1678

Contents?: true

Size: 885 Bytes

Versions: 1

Compression:

Stored size: 885 Bytes

Contents

class Hand

  include Enumerable

  attr_reader :cards

  # initialize with an array of cards
  def initialize(cards = [])
    @cards = []
    cards.each do |card|
      self << card
    end
  end

  # add a card if it is valid
  def add(card)
    @cards << card if card.instance_of? Card
  end

  # alias array push (<<) to add method
  alias_method :<<, :add

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

  # sorting
  def sort!
    @cards.sort!
  end

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

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

  # displaying the card icons
  # using extensions/string.rb for String#next
  def to_s
    @cards.map(&:to_s).inject(:next)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubycards-0.0.1 lib/rubycards/hand.rb