Class: Card
- Inherits:
-
Object
- Object
- Card
- Extended by:
- T::Sig
- Defined in:
- lib/deck_of_cards_handler/card/card.rb
Overview
This represents a Playing Card. It has the following properties: a suit, a value and a position.
Instance Attribute Summary collapse
-
#position ⇒ Object
Returns the value of attribute position.
-
#suit ⇒ Object
Returns the value of attribute suit.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
- .black_suits ⇒ Object
- .court_values ⇒ Object
- .even_values ⇒ Object
- .odd_values ⇒ Object
- .red_suits ⇒ Object
- .spot_values ⇒ Object
- .suits ⇒ Object
- .values ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #black? ⇒ Boolean
- #clubs? ⇒ Boolean
- #color ⇒ Object
- #court? ⇒ Boolean
- #diamonds? ⇒ Boolean
- #even? ⇒ Boolean
- #hearts? ⇒ Boolean
-
#initialize(suit:, value:, position: nil) ⇒ Card
constructor
A new instance of Card.
- #odd? ⇒ Boolean
- #rank(low_ace: false) ⇒ Object
- #red? ⇒ Boolean
- #spades? ⇒ Boolean
- #spot? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(suit:, value:, position: nil) ⇒ Card
Returns a new instance of Card.
19 20 21 22 23 24 25 26 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 19 def initialize(suit:, value:, position: nil) raise ArgumentError, "Invalid suit" unless Card.suits.include?(suit) raise ArgumentError, "Invalid value" unless Card.values.include?(value) @suit = T.let(suit, String) @value = T.let(value, String) @position = T.let(position, T.nilable(Integer)) end |
Instance Attribute Details
#position ⇒ Object
Returns the value of attribute position.
16 17 18 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 16 def position @position end |
#suit ⇒ Object
Returns the value of attribute suit.
10 11 12 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 10 def suit @suit end |
#value ⇒ Object
Returns the value of attribute value.
13 14 15 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 13 def value @value end |
Class Method Details
.black_suits ⇒ Object
123 124 125 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 123 def black_suits %w[S C].freeze end |
.court_values ⇒ Object
143 144 145 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 143 def court_values %w[J Q K].freeze end |
.even_values ⇒ Object
138 139 140 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 138 def even_values %w[2 4 6 8 10 Q].freeze end |
.odd_values ⇒ Object
133 134 135 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 133 def odd_values %w[A 3 5 7 9 J K].freeze end |
.red_suits ⇒ Object
118 119 120 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 118 def red_suits %w[H D].freeze end |
.spot_values ⇒ Object
148 149 150 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 148 def spot_values %w[A 2 3 4 5 6 7 8 9 10].freeze end |
.suits ⇒ Object
113 114 115 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 113 def suits %w[C H S D].freeze end |
.values ⇒ Object
128 129 130 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 128 def values %w[A 2 3 4 5 6 7 8 9 10 J Q K].freeze end |
Instance Method Details
#<=>(other) ⇒ Object
105 106 107 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 105 def <=>(other) rank <=> other.rank end |
#==(other) ⇒ Object
89 90 91 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 89 def ==(other) other.suit == suit && other.value == value end |
#black? ⇒ Boolean
39 40 41 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 39 def black? Card.black_suits.include?(suit) end |
#clubs? ⇒ Boolean
44 45 46 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 44 def clubs? suit == "C" end |
#color ⇒ Object
29 30 31 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 29 def color red? ? "red" : "black" end |
#court? ⇒ Boolean
79 80 81 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 79 def court? Card.court_values.include?(value) end |
#diamonds? ⇒ Boolean
59 60 61 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 59 def diamonds? suit == "D" end |
#even? ⇒ Boolean
64 65 66 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 64 def even? Card.even_values.include?(value) end |
#hearts? ⇒ Boolean
49 50 51 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 49 def hearts? suit == "H" end |
#odd? ⇒ Boolean
69 70 71 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 69 def odd? Card.odd_values.include?(value) end |
#rank(low_ace: false) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 94 def rank(low_ace: false) case value when "A" then low_ace ? 1 : 14 when "J" then 11 when "Q" then 12 when "K" then 13 else value.to_i end end |
#red? ⇒ Boolean
34 35 36 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 34 def red? Card.red_suits.include?(suit) end |
#spades? ⇒ Boolean
54 55 56 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 54 def spades? suit == "S" end |
#spot? ⇒ Boolean
74 75 76 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 74 def spot? Card.spot_values.include?(value) end |
#to_s ⇒ Object
84 85 86 |
# File 'lib/deck_of_cards_handler/card/card.rb', line 84 def to_s "#{value} of #{suit}" end |