Skip to content

Instantly share code, notes, and snippets.

@leejarvis

leejarvis/blah Secret

Created February 21, 2012 09:46
Show Gist options
  • Save leejarvis/f2e1777cb8c75026efb0 to your computer and use it in GitHub Desktop.
Save leejarvis/f2e1777cb8c75026efb0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Scanner
KEYWORDS = {
:direction => %w( north south east west etc ),
:verb => %w( go stop kill eat ),
:stop => %w( the in of from at it ),
:noun => %w( door bear princess cabinet )
}
Token = Struct.new(:type, :word)
def self.scan(string)
string.split.map do |word|
if key = KEYWORDS.find { |k, v| v.include?(word) }
Token.new key.shift, word
end
end
end
end
p Scanner.scan 'south' #=> [#<struct Scanner::Token type=:direction, word="south">]
p Scanner.scan 'door' #=> [#<struct Scanner::Token type=:noun, word="door">]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment