Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created June 8, 2014 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/bbfbbd71956d90bba8f2 to your computer and use it in GitHub Desktop.
Save havenwood/bbfbbd71956d90bba8f2 to your computer and use it in GitHub Desktop.
Rock, Paper, Scissors
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