user1070381
user1070381

Reputation: 503

Learn Ruby Hard Way ex. 48

I'm endeavoring to learn Ruby, and have made it through most of Learn Ruby the Hard Way by Zed Shaw, but this latest exercise has me completely stumped. It is a reverse exercise of sorts, getting you to create a class Lexicon that can get tested by the code provided.

You're supposed to create Lexicon so that it can go through user input and get various bits of data from it. So far all I have for testing direction input, for example is:

class Lexicon

Pair = Struct.new(:qualifier, :value)
userinput = gets.chomp()
userwords = userinput.split()

for i in userwords
    if userwords[i].include?("north", "south", "east", "west")
        directions = Pair.new("direction", userwords[i])
    else
        i++
    end
end     
end

The corresponding testing code is:

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

Thank you all in advance for the help. I know I'm probably way off, but am trying to go through the home stretch of learning Ruby the Hard Way!

Upvotes: 2

Views: 923

Answers (1)

Saeed Gatson
Saeed Gatson

Reputation: 517

You should wrap your for loop in a scan method because that's what the test case is expecting. The scan method also needs to return a list of 'Pairs'.

Try something like this.

def scan(userwords)
  sentence = Array.new
  userwords.each { |word|
    if word.include?("north", "south", "east", "west")
      sentence.push(Pair.new(:direction, word))
  }
  sentence
end 

That should help get you started in the right direction.

Upvotes: 1

Related Questions