Class: Zheng::Rating
- Inherits:
-
Object
- Object
- Zheng::Rating
- Defined in:
- lib/zheng/rating.rb
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #from_rank(rank) ⇒ Object
-
#initialize(rating) ⇒ Rating
constructor
A new instance of Rating.
- #rank ⇒ Object
- #to_i ⇒ Object
Constructor Details
#initialize(rating) ⇒ Rating
Returns a new instance of Rating.
3 4 5 |
# File 'lib/zheng/rating.rb', line 3 def initialize() @rating = .respond_to?(:match) ? from_rank() : .to_i end |
Class Method Details
.is_rank?(string) ⇒ Boolean
36 37 38 39 40 |
# File 'lib/zheng/rating.rb', line 36 def self.is_rank? string return false unless string.respond_to? :match return false unless string.match /\d+(k|d)/ return true end |
Instance Method Details
#+(other) ⇒ Object
24 25 26 |
# File 'lib/zheng/rating.rb', line 24 def + other self.class.new(@rating + other.to_i) end |
#==(other) ⇒ Object
28 29 30 |
# File 'lib/zheng/rating.rb', line 28 def == other @rating == other.to_i end |
#eql?(other) ⇒ Boolean
32 33 34 |
# File 'lib/zheng/rating.rb', line 32 def eql? other @rating == other.to_i end |
#from_rank(rank) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/zheng/rating.rb', line 7 def from_rank(rank) parts = rank.match(/(\d+)(k|d)/) return 2000 + (parts[1].to_i * 100) if parts[2] == 'd' return 2100 - (parts[1].to_i * 100) if parts[2] == 'k' raise "Invalid rank" end |
#rank ⇒ Object
18 19 20 21 22 |
# File 'lib/zheng/rating.rb', line 18 def rank level = (@rating / 100.0).round return (level - 20).to_s + 'd' if level > 20 return (21 - level).to_s + 'k' end |
#to_i ⇒ Object
14 15 16 |
# File 'lib/zheng/rating.rb', line 14 def to_i @rating end |