Skip to content

Instantly share code, notes, and snippets.

@jeremyvdw
Created August 27, 2014 08:27
Show Gist options
  • Save jeremyvdw/d6970ebfea4e3e0d9c30 to your computer and use it in GitHub Desktop.
Save jeremyvdw/d6970ebfea4e3e0d9c30 to your computer and use it in GitHub Desktop.
# Holds the current point at any given moment
# set: the set we're in
# game: the game we're in
# point: the point we're in (aggregated)
#
# Ex:
# +----------+---+---+---+----+
# | Nadal* | 6 | 4 | 5 | 30 |
# +----------+---+---+---+----+ ==> Nadal is serving for: Point.new(3,8,4)
# | Djokovic | 4 | 6 | 2 | 15 |
# +----------+---+---+---+----+
#
module Tennis
class Point
include Comparable
attr_reader :set, :game, :point
def initialize(set, game, point)
@set = Integer(set)
@game = Integer(game)
@point = Integer(point)
self.freeze
end
SLASH = '/'.freeze
def to_s
to_ary.join(SLASH)
end
def to_ary
[set, game, point]
end
alias_method :mongoize, :to_ary
def self.demongoize(ary)
new(*ary) if ary
end
def ==(other)
return false unless Point === other
to_ary == other.to_ary
end
alias :eql? :==
def <=>(other)
fail unless Point === other
to_ary <=> other.to_ary
end
def first_game_of_match?
set == 1 && game == 1
end
def inspect
"#<Point #{to_s}>"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment