Guest User

Untitled

a guest
Feb 19th, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. class WeatherBot
  2. def name; self.class.name; end
  3. def play(last=nil)
  4. if !last or last == '' then 'r'
  5. else @always_play ||= beats(last) end
  6. end
  7. def beats(play)
  8. if paper?(play) then 's'
  9. elsif rock?(play) then 'p'
  10. else 'r' end
  11. end
  12. %w(paper rock scissors).each do |e|
  13. define_method("#{e}?"){|i| (e[0,1] == i[0,1].downcase)}
  14. end
  15. end
  16.  
  17. if __FILE__ == $0
  18. require 'test/unit'
  19. class WeatherBotTest < Test::Unit::TestCase
  20. def test_beats
  21. assert_equal 'r', w.beats('s')
  22. assert_equal 'p', w.beats('r')
  23. assert_equal 's', w.beats('p')
  24. end
  25. def test_play
  26. assert_equal 'r', w.play
  27. assert_equal 'r', w.play('')
  28. assert_equal 's', w.play('p')
  29. assert_equal 's', w.play('r')
  30. assert_equal 's', w.play('r')
  31. assert_equal 's', w.play('s')
  32. end
  33. def test_check_methods
  34. assert w.paper?('Paper')
  35. assert !w.paper?('r')
  36. assert w.rock?('r')
  37. assert !w.rock?('Scissors')
  38. assert w.scissors?('scisors')
  39. assert !w.scissors?('P')
  40. end
  41. def w; @w ||= WeatherBot.new; end
  42. end
  43. end
Add Comment
Please, Sign In to add comment