public
Last active

  • Download Gist
weather_bot.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.