class CardsLib::Deck
Public Class Methods
new(options = {})
click to toggle source
# File lib/cards_lib/deck.rb, line 3 def initialize(options = {}) cards = options.fetch(:cards) { Standard::PLAYING_CARDS } ranker = options.fetch(:ranker){ Ranker } @seed = options.fetch(:seed) { Random.new.seed } @top = 0 @cards = if cards.all? {|c| c.is_a? Card } cards else cards.map {|c| Card.new(c, ranker) } end.shuffle(random: Random.new(@seed)).to_enum end
Public Instance Methods
cards()
click to toggle source
# File lib/cards_lib/deck.rb, line 19 def cards @cards end
count()
click to toggle source
# File lib/cards_lib/deck.rb, line 49 def count size end
empty?()
click to toggle source
# File lib/cards_lib/deck.rb, line 37 def empty? !peak end
face_up()
click to toggle source
# File lib/cards_lib/deck.rb, line 53 def face_up @cards.map(&:face) end
inspect()
click to toggle source
# File lib/cards_lib/deck.rb, line 15 def inspect "<Deck: #{size} Cards - Seed##{@seed}>" end
peak()
click to toggle source
# File lib/cards_lib/deck.rb, line 23 def peak Array(_cards[@top..@top]).first end
pluck()
click to toggle source
# File lib/cards_lib/deck.rb, line 27 def pluck card = peak @top += 1 unless @top == _cards.size card || card.tap {|i| i.define_singleton_method(:face) { nil } } end
present?()
click to toggle source
# File lib/cards_lib/deck.rb, line 41 def present? !empty? end
return_card()
click to toggle source
# File lib/cards_lib/deck.rb, line 33 def return_card @top -= 1 unless @top == 0 end
size()
click to toggle source
# File lib/cards_lib/deck.rb, line 45 def size _cards[@top..-1].size end
Private Instance Methods
_cards()
click to toggle source
# File lib/cards_lib/deck.rb, line 59 def _cards @cards.to_a end