Class: Cardistry::Deck

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cardistry/deck.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDeck

Returns a new instance of Deck.



10
11
12
13
# File 'lib/cardistry/deck.rb', line 10

def initialize
  @cards = []
  @suits = []
end

Instance Attribute Details

#suitsObject (readonly)

Returns the value of attribute suits.



8
9
10
# File 'lib/cardistry/deck.rb', line 8

def suits
  @suits
end

Instance Method Details

#[](index) ⇒ Object



34
35
36
# File 'lib/cardistry/deck.rb', line 34

def [] index
  @cards[index] || "Not found"
end

#each(&block) ⇒ Object



30
31
32
# File 'lib/cardistry/deck.rb', line 30

def each &block
  @cards.each &block
end

#infoObject



42
43
44
45
46
47
# File 'lib/cardistry/deck.rb', line 42

def info
  info = <<~INFO
    cards: #{size}
    suits: #{@suits}
  INFO
end

#load(from_file) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cardistry/deck.rb', line 15

def load from_file
  data = File.read from_file

  JSON.parse(data, { symbolize_names: true }).each do |entry|
    rank = entry[:rank]
    suit = entry[:suit] ? entry[:suit].to_sym : nil
    kind = entry[:kind] ? entry[:kind].to_sym : nil
    name = entry[:name]

    card = Card.new( rank, suit, kind, name )
    register_suit card.suit
    @cards << card
  end
end

#sizeObject



38
39
40
# File 'lib/cardistry/deck.rb', line 38

def size
  @cards.size
end