Advertisement
Guest User

Untitled

a guest
Feb 17th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. I am trying to create an ex48.rb based on a TEST
  2. This is an assignment from the following website:
  3. http://ruby.learncodethehardway.org/book/ex48.html
  4.  
  5. HEre what I've done so far without success
  6. --------------------------------------------------------------
  7.  
  8.  
  9. # ex48.rb
  10. module Lexicon
  11. class Pair
  12.  
  13. attr_accessor :token, :word
  14. # the token are: direction, verb, stop, noun, number
  15. # the words are the words from those tokens
  16. def initialize(token, word)
  17. @token = token
  18. @word = word
  19. end
  20.  
  21.  
  22. def scan(stuff)
  23. @words = stuff.split()
  24. if @words.include? @word
  25. return token
  26. else
  27. puts "The word %s was not found" % @token
  28. end
  29. end
  30.  
  31. def add_words(words)
  32. @words.update(words)
  33. end
  34.  
  35. stuf = gets.comp()
  36. end
  37.  
  38.  
  39. --------
  40. # /lib/lexicon.rb
  41. Direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right']
  42. Verbs = ['go', 'stop', 'kill', 'eat']
  43. Stop_words = ['the', 'in', 'of', 'from', 'at', 'it']
  44. Nouns = ['door', 'bear', 'princess', 'cabinet']
  45.  
  46. ---------
  47.  
  48. ERROR MESSAGE I'M GETTING BELLOW
  49.  
  50. ----------
  51.  
  52.  
  53. osiris:test Richardson$ ruby test_lexicon.rb
  54. test_lexicon.rb:6:in `<class:LexiconTests>': uninitialized constant LexiconTests::Lexicon (NameError)
  55. from test_lexicon.rb:4:in `<main>'
  56. osiris:test Richardson$
  57.  
  58. -----------
  59.  
  60. TEST file Below
  61.  
  62. ------------
  63.  
  64. require 'test/unit'
  65. require_relative "../lib/lexicon"
  66.  
  67. class LexiconTests < Test::Unit::TestCase
  68.  
  69. Pair = Lexicon::Pair
  70. @@lexicon = Lexicon.new()
  71.  
  72. def test_directions()
  73. assert_equal([Pair.new(:direction, 'north')], @@lexicon.scan("north"))
  74. result = @@lexicon.scan("north south east")
  75. assert_equal(result, [Pair.new(:direction, 'north'),
  76. Pair.new(:direction, 'south'),
  77. Pair.new(:direction, 'east')])
  78. end
  79.  
  80. def test_verbs()
  81. assert_equal(@@lexicon.scan("go"), [Pair.new(:verb, 'go')])
  82. result = @@lexicon.scan("go kill eat")
  83. assert_equal(result, [Pair.new(:verb, 'go'),
  84. Pair.new(:verb, 'kill'),
  85. Pair.new(:verb, 'eat')])
  86. end
  87.  
  88. def test_stops()
  89. assert_equal(@@lexicon.scan("the"), [Pair.new(:stop, 'the')])
  90. result = @@lexicon.scan("the in of")
  91. assert_equal(result, [Pair.new(:stop, 'the'),
  92. Pair.new(:stop, 'in'),
  93. Pair.new(:stop, 'of')])
  94. end
  95.  
  96. def test_nouns()
  97. assert_equal(@@lexicon.scan("bear"), [Pair.new(:noun, 'bear')])
  98. result = @@lexicon.scan("bear princess")
  99. assert_equal(result, [Pair.new(:noun, 'bear'),
  100. Pair.new(:noun, 'princess')])
  101. end
  102.  
  103. def test_numbers()
  104. assert_equal(@@lexicon.scan("1234"), [Pair.new(:number, 1234)])
  105. result = @@lexicon.scan("3 91234")
  106. assert_equal(result, [Pair.new(:number, 3),
  107. Pair.new(:number, 91234)])
  108. end
  109.  
  110. def test_errors()
  111. assert_equal(@@lexicon.scan("ASDFADFASDF"), [Pair.new(:error, 'ASDFADFASDF')])
  112. result = @@lexicon.scan("bear IAS princess")
  113. assert_equal(result, [Pair.new(:noun, 'bear'),
  114. Pair.new(:error, 'IAS'),
  115. Pair.new(:noun, 'princess')])
  116. end
  117.  
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement