Class: CardsLib::Ranker
- Inherits:
-
Object
show all
- Includes:
- Comparable
- Defined in:
- lib/cards_lib/ranker.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(rank = nil, ranks = nil, rank_lookup = nil) ⇒ Ranker
Initialize’s arguments: rank - is whatever part of the object, or the object itself you choose to use. ranks - is a simple hash to lookup a cards value with the keys matching the rank object. rank_lookup - is an optional Proc to redefine how you will lookup a cards value.
9
10
11
12
13
|
# File 'lib/cards_lib/ranker.rb', line 9
def initialize(rank = nil, ranks = nil, rank_lookup = nil)
@rank = rank
@ranks = ranks
@rank_lookup = rank_lookup
end
|
Instance Attribute Details
#rank ⇒ Object
Returns the value of attribute rank.
4
5
6
|
# File 'lib/cards_lib/ranker.rb', line 4
def rank
@rank
end
|
#rank_lookup ⇒ Object
Returns the value of attribute rank_lookup.
4
5
6
|
# File 'lib/cards_lib/ranker.rb', line 4
def rank_lookup
@rank_lookup
end
|
Instance Method Details
#<=>(other) ⇒ Object
27
28
29
|
# File 'lib/cards_lib/ranker.rb', line 27
def <=>(other)
ranker(rank) <=> ranker(other.rank)
end
|
#ranker(rank_face = @rank) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/cards_lib/ranker.rb', line 19
def ranker(rank_face = @rank)
if @rank_lookup
@rank_lookup.(rank_face)
else
ranks.index(rank_face).to_i + 1
end
end
|
#ranks ⇒ Object
15
16
17
|
# File 'lib/cards_lib/ranker.rb', line 15
def ranks
@ranks || Standard::RANKS
end
|
#sequential?(other) ⇒ Boolean
31
32
33
|
# File 'lib/cards_lib/ranker.rb', line 31
def sequential?(other)
(ranker(rank) - ranker(other.rank)).abs == 1
end
|