-
-
Save leejarvis/8bdc156986f026950418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class Lexicon | |
Pair = Struct.new(:token, :word) | |
@lexicon = { | |
'north' => :direction, | |
'south' => :direction, | |
} | |
def self.scan(string) | |
words = string.split | |
words.map do |word| | |
Pair.new(@lexicon[word], word) if @lexicon.key?(word) | |
end | |
end | |
end | |
p Lexicon.scan 'north' #=> [#<struct Lexicon::Pair token=:direction, word="north">] | |
p Lexicon.scan 'north south' #=> [#<struct Lexicon::Pair token=:direction, word="north">, #<struct Lexicon::Pair token=:direction, word="south">] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment