Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I am trying to create an ex48.rb based on a TEST
- This is an assignment from the following website:
- http://ruby.learncodethehardway.org/book/ex48.html
- HEre what I've done so far without success
- --------------------------------------------------------------
- # ex48.rb
- module Lexicon
- class Pair
- attr_accessor :token, :word
- # the token are: direction, verb, stop, noun, number
- # the words are the words from those tokens
- def initialize(token, word)
- @token = token
- @word = word
- end
- def scan(stuff)
- @words = stuff.split()
- if @words.include? @word
- return token
- else
- puts "The word %s was not found" % @token
- end
- end
- def add_words(words)
- @words.update(words)
- end
- stuf = gets.comp()
- end
- --------
- # /lib/lexicon.rb
- Direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right']
- Verbs = ['go', 'stop', 'kill', 'eat']
- Stop_words = ['the', 'in', 'of', 'from', 'at', 'it']
- Nouns = ['door', 'bear', 'princess', 'cabinet']
- ---------
- ERROR MESSAGE I'M GETTING BELLOW
- ----------
- osiris:test Richardson$ ruby test_lexicon.rb
- test_lexicon.rb:6:in `<class:LexiconTests>': uninitialized constant LexiconTests::Lexicon (NameError)
- from test_lexicon.rb:4:in `<main>'
- osiris:test Richardson$
- -----------
- TEST file Below
- ------------
- require 'test/unit'
- require_relative "../lib/lexicon"
- class LexiconTests < Test::Unit::TestCase
- Pair = Lexicon::Pair
- @@lexicon = Lexicon.new()
- def test_directions()
- assert_equal([Pair.new(:direction, 'north')], @@lexicon.scan("north"))
- result = @@lexicon.scan("north south east")
- assert_equal(result, [Pair.new(:direction, 'north'),
- Pair.new(:direction, 'south'),
- Pair.new(:direction, 'east')])
- end
- def test_verbs()
- assert_equal(@@lexicon.scan("go"), [Pair.new(:verb, 'go')])
- result = @@lexicon.scan("go kill eat")
- assert_equal(result, [Pair.new(:verb, 'go'),
- Pair.new(:verb, 'kill'),
- Pair.new(:verb, 'eat')])
- end
- def test_stops()
- assert_equal(@@lexicon.scan("the"), [Pair.new(:stop, 'the')])
- result = @@lexicon.scan("the in of")
- assert_equal(result, [Pair.new(:stop, 'the'),
- Pair.new(:stop, 'in'),
- Pair.new(:stop, 'of')])
- end
- def test_nouns()
- assert_equal(@@lexicon.scan("bear"), [Pair.new(:noun, 'bear')])
- result = @@lexicon.scan("bear princess")
- assert_equal(result, [Pair.new(:noun, 'bear'),
- Pair.new(:noun, 'princess')])
- end
- def test_numbers()
- assert_equal(@@lexicon.scan("1234"), [Pair.new(:number, 1234)])
- result = @@lexicon.scan("3 91234")
- assert_equal(result, [Pair.new(:number, 3),
- Pair.new(:number, 91234)])
- end
- def test_errors()
- assert_equal(@@lexicon.scan("ASDFADFASDF"), [Pair.new(:error, 'ASDFADFASDF')])
- result = @@lexicon.scan("bear IAS princess")
- assert_equal(result, [Pair.new(:noun, 'bear'),
- Pair.new(:error, 'IAS'),
- Pair.new(:noun, 'princess')])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement