/
deck.rb
45 lines (33 loc) · 1.13 KB
/
deck.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Deck
attr_accessor :cards, :new_card
attr_reader :filename
def initialize(filename)
@filename = filename
@cards = []
load_cards
end
def load_cards #eliminated creating the slices array and then having to iterate over that to create new Card objects. Just grabbed the slices from the text file and for each slice created a new Card object with a hash containing the term and defintion attributes.
file = File.open(filename)
file.readlines.each_slice(3) do |slice|
card_attributes = {:term => slice[1].chomp, :definition => slice[0].chomp}
self.cards << new_object(Card, card_attributes)
end
end
def new_object(klass,options)
klass.new(options)
end
def delete_card_from_deck(term_to_delete)
self.cards.delete_if { |card| card.attributes[:term] == term_to_delete }
end
def pull_random_card
random_card = self.cards.sample
random_card.attributes
end
end
class Card
attr_reader :attributes
def initialize(attributes)
@attributes = attributes
end
end
#RENAMED OPTIONS AND HASH TO BE MORE DESCRIPTIVE VARIABLE AND HASH NAMES (card_attributes and attributes(hash))