-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard.rb
More file actions
executable file
·45 lines (35 loc) · 830 Bytes
/
Copy pathcard.rb
File metadata and controls
executable file
·45 lines (35 loc) · 830 Bytes
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
#!/usr/bin/ruby
# card.rb
class Card
attr_accessor :suit, :number
def initialize(suit, number)
self.suit = suit unless suit < 0 || suit > 3
self.number = number unless number < 0 || number > 12
end
SUITS = %w(Spaids Hearts Clubs Dimonds)
NUMBERS = %w(Ace 2 3 4 5 6 7 8 9 10 Jack Queen King)
def to_s
suit = self.suit.to_i unless @suit.nil?
number = self.number.to_i unless @number.nil?
if valid?
"#{NUMBERS[number]} of #{SUITS[suit]}"
else
'invalid card'
end
end
def ==(other)
number == other.number
end
def !=(other)
number != other.number
end
def valid?
valid_suit? && valid_number?
end
def valid_number?
(number >= 0 && number <= 12) unless number.nil?
end
def valid_suit?
(suit >= 0 && suit <= 3) unless suit.nil?
end
end