Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment