Skip to content

Instantly share code, notes, and snippets.

@leejarvis

leejarvis/blah Secret

Created February 21, 2012 09:29
Show Gist options
  • Save leejarvis/8bdc156986f026950418 to your computer and use it in GitHub Desktop.
Save leejarvis/8bdc156986f026950418 to your computer and use it in GitHub Desktop.
#!/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