Created
June 8, 2014 15:36
-
-
Save havenwood/bbfbbd71956d90bba8f2 to your computer and use it in GitHub Desktop.
Rock, Paper, Scissors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RockPaperScissors | |
OPTIONS = [:rock, :paper, :scissors] | |
include Comparable | |
attr_reader :hand | |
def initialize | |
@hand = OPTIONS.sample | |
end | |
def <=> other | |
mine, yours = self.hand, other.hand | |
if mine == yours | |
0 | |
elsif OPTIONS.rotate[OPTIONS.index(mine)] == yours | |
-1 | |
else | |
1 | |
end | |
end | |
end | |
hand1 = RockPaperScissors.new | |
#=> #<RockPaperScissors:0x007fc11417f4a8 @hand=:rock> | |
hand2 = RockPaperScissors.new | |
#=> #<RockPaperScissors:0x007fc116ac4728 @hand=:paper> | |
hand1 > hand2 | |
#=> false | |
hand2 > hand1 | |
#=> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment