Class: Card
Constant Summary collapse
- SUITS =
"cdhs"- FACES =
"L23456789TJQKA"- SUIT_LOOKUP =
{ 'c' => 0, 'd' => 1, 'h' => 2, 's' => 3, 'C' => 0, 'D' => 1, 'H' => 2, 'S' => 3, }
- FACE_VALUES =
{ 'L' => 1, # this is a magic low ace '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'T' => 10, 'J' => 11, 'Q' => 12, 'K' => 13, 'A' => 14, }
Instance Attribute Summary collapse
-
#face ⇒ Object
readonly
Returns the value of attribute face.
-
#suit ⇒ Object
readonly
Returns the value of attribute suit.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(card2) ⇒ Object
Compare the face value of this card with another card.
-
#==(card2) ⇒ Object
(also: #eql?)
Returns true if the cards are the same card.
-
#hash ⇒ Object
Compute a hash-code for this Card.
-
#initialize(*value) ⇒ Card
constructor
A new instance of Card.
-
#to_card ⇒ Object
If to_card is called on a
Cardit should return itself. -
#to_s ⇒ Object
Returns a string containing the representation of Card.
Constructor Details
#initialize(*value) ⇒ Card
Returns a new instance of Card.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/card.rb', line 70 def initialize(*value) if (value.size == 1) if (value[0].respond_to?(:to_card)) build_from_card(value[0]) elsif (value[0].respond_to?(:to_str)) build_from_string(value[0]) elsif (value[0].respond_to?(:to_int)) build_from_value(value[0]) end elsif (value.size == 2) if (value[0].respond_to?(:to_str) && value[1].respond_to?(:to_str)) build_from_face_suit(value[0], value[1]) elsif (value[0].respond_to?(:to_int) && value[1].respond_to?(:to_int)) build_from_face_suit_values(value[0], value[1]) end end end |
Instance Attribute Details
#face ⇒ Object (readonly)
Returns the value of attribute face.
90 91 92 |
# File 'lib/card.rb', line 90 def face @face end |
#suit ⇒ Object (readonly)
Returns the value of attribute suit.
90 91 92 |
# File 'lib/card.rb', line 90 def suit @suit end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
90 91 92 |
# File 'lib/card.rb', line 90 def value @value end |
Class Method Details
.face_value(face) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/card.rb', line 31 def Card.face_value(face) if (face) FACE_VALUES[face.upcase] - 1 else nil end end |
Instance Method Details
#<=>(card2) ⇒ Object
Compare the face value of this card with another card. Returns: -1 if self is less than card2 0 if self is the same face value of card2 1 if self is greater than card2
109 110 111 |
# File 'lib/card.rb', line 109 def <=> card2 @face <=> card2.face end |
#==(card2) ⇒ Object Also known as: eql?
Returns true if the cards are the same card. Meaning they have the same suit and the same face value.
115 116 117 |
# File 'lib/card.rb', line 115 def == card2 @value == card2.value end |
#hash ⇒ Object
Compute a hash-code for this Card. Two Cards with the same content will have the same hash code (and will compare using eql?).
122 123 124 |
# File 'lib/card.rb', line 122 def hash @value.hash end |
#to_card ⇒ Object
If to_card is called on a Card it should return itself
101 102 103 |
# File 'lib/card.rb', line 101 def to_card self end |
#to_s ⇒ Object
Returns a string containing the representation of Card
Card.new(“7c”).to_s # => “7c”
96 97 98 |
# File 'lib/card.rb', line 96 def to_s FACES[@face].chr + SUITS[@suit].chr end |