Class: CardsLib::Card
- Inherits:
-
Object
- Object
- CardsLib::Card
- Includes:
- Comparable
- Defined in:
- lib/cards_lib/card.rb
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
equal by both rank and suit.
-
#eql?(other) ⇒ Boolean
equal by value.
- #face ⇒ Object
-
#initialize(face, ranker = Ranker) ⇒ Card
constructor
A new instance of Card.
- #inspect ⇒ Object
-
#ordered?(other) ⇒ Boolean
returns other if true.
- #pair?(other) ⇒ Boolean
-
#paired?(other) ⇒ Boolean
return other if true.
- #rank ⇒ Object
- #sequential?(other) ⇒ Boolean
- #suit ⇒ Object
-
#suited?(other) ⇒ Boolean
return other if true.
- #value ⇒ Object
Constructor Details
#initialize(face, ranker = Ranker) ⇒ Card
Returns a new instance of Card.
4 5 6 7 8 9 10 11 12 |
# File 'lib/cards_lib/card.rb', line 4 def initialize(face, ranker = Ranker) raise InvalidCardFace, "Parameter face cannot be blank!" if face.to_s.empty? @suit = if_hash_then_fetch(face, :suit) @rank = if_hash_then_fetch(face, :rank) @face = face_from_rank_and_suit(@rank, @suit) if face.is_a? Hash @face ||= face @ranker = ranker.new(self.rank) end |
Instance Method Details
#<=>(other) ⇒ Object
48 49 50 |
# File 'lib/cards_lib/card.rb', line 48 def <=>(other) @ranker.<=>(other) end |
#==(other) ⇒ Object
equal by both rank and suit
39 40 41 |
# File 'lib/cards_lib/card.rb', line 39 def ==(other) pair?(other) && self.suit == other.suit end |
#eql?(other) ⇒ Boolean
equal by value
44 45 46 |
# File 'lib/cards_lib/card.rb', line 44 def eql?(other) value == other.value end |
#face ⇒ Object
18 19 20 |
# File 'lib/cards_lib/card.rb', line 18 def face @face end |
#inspect ⇒ Object
14 15 16 |
# File 'lib/cards_lib/card.rb', line 14 def inspect "(Card)" end |
#ordered?(other) ⇒ Boolean
returns other if true
67 68 69 |
# File 'lib/cards_lib/card.rb', line 67 def ordered?(other) self.sequential?(other) ? other : nil end |
#pair?(other) ⇒ Boolean
34 35 36 |
# File 'lib/cards_lib/card.rb', line 34 def pair?(other) self.rank == other.rank end |
#paired?(other) ⇒ Boolean
return other if true
57 58 59 |
# File 'lib/cards_lib/card.rb', line 57 def paired?(other) (self.rank == other.rank) ? other : nil end |
#rank ⇒ Object
26 27 28 |
# File 'lib/cards_lib/card.rb', line 26 def rank @rank || @face[0] end |
#sequential?(other) ⇒ Boolean
52 53 54 |
# File 'lib/cards_lib/card.rb', line 52 def sequential?(other) @ranker.sequential?(other) end |
#suit ⇒ Object
22 23 24 |
# File 'lib/cards_lib/card.rb', line 22 def suit @suit || @face[1..-1] end |
#suited?(other) ⇒ Boolean
return other if true
62 63 64 |
# File 'lib/cards_lib/card.rb', line 62 def suited?(other) (self.suit == other.suit) ? other : nil end |
#value ⇒ Object
30 31 32 |
# File 'lib/cards_lib/card.rb', line 30 def value @ranker.ranker end |