Skip to content

Instantly share code, notes, and snippets.

@ntalbott
Created August 9, 2008 18:40
Show Gist options
  • Save ntalbott/4696 to your computer and use it in GitHub Desktop.
Save ntalbott/4696 to your computer and use it in GitHub Desktop.
class WeatherBot
def name; self.class.name; end
def play(last=nil)
if !last or last == '' then 'r'
else @always_play ||= beats(last) end
end
def beats(play)
if paper?(play) then 's'
elsif rock?(play) then 'p'
else 'r' end
end
%w(paper rock scissors).each do |e|
define_method("#{e}?"){|i| (e[0,1] == i[0,1].downcase)}
end
end
if __FILE__ == $0
require 'test/unit'
class WeatherBotTest < Test::Unit::TestCase
def test_beats
assert_equal 'r', w.beats('s')
assert_equal 'p', w.beats('r')
assert_equal 's', w.beats('p')
end
def test_play
assert_equal 'r', w.play
assert_equal 'r', w.play('')
assert_equal 's', w.play('p')
assert_equal 's', w.play('r')
assert_equal 's', w.play('r')
assert_equal 's', w.play('s')
end
def test_check_methods
assert w.paper?('Paper')
assert !w.paper?('r')
assert w.rock?('r')
assert !w.rock?('Scissors')
assert w.scissors?('scisors')
assert !w.scissors?('P')
end
def w; @w ||= WeatherBot.new; end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment