Class: RubyCards::Deck

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/rubycards/deck.rb

Constant Summary collapse

RANKS =
[*2..10, 'Jack', 'Queen', 'King', 'Ace']
SUITS =
%w{ Clubs Diamonds Hearts Spades }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDeck

Initializes a standard deck of 52 cards



21
22
23
24
25
26
27
# File 'lib/rubycards/deck.rb', line 21

def initialize
  @cards = []

  RANKS.product(SUITS).each do |rank, suit|
    @cards << Card.new(rank, suit)
  end
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



9
10
11
# File 'lib/rubycards/deck.rb', line 9

def cards
  @cards
end

Instance Method Details

#each(&block) ⇒ Enumerable

Enumerates the deck

Parameters:

  • block (Proc)

    The block to pass into the enumerator

Returns:

  • (Enumerable)

    The deck enumerator



41
42
43
# File 'lib/rubycards/deck.rb', line 41

def each(&block)
  @cards.each(&block)
end

#inspectString

Displays a shortened version of the #to_s method for use in the ruby console

Returns:

  • (String)

    A shortened string output of the deck



56
57
58
# File 'lib/rubycards/deck.rb', line 56

def inspect
  "[ #{@cards[0..2].map(&:inspect).join ', '}, ..., #{@cards[-3..-1].map(&:inspect).join ', '} ]"
end

#shuffle!Deck

Shuffles the deck and returns it

Returns:

  • (Deck)

    The shuffled deck



32
33
34
35
# File 'lib/rubycards/deck.rb', line 32

def shuffle!
  @cards.shuffle!
  self
end

#to_sString

Displays concise card representations in an array

Returns:

  • (String)

    The concise string representation of the deck



48
49
50
# File 'lib/rubycards/deck.rb', line 48

def to_s
  "[ #{@cards.map(&:inspect).join ', '} ]"
end