class Textoken::Scanner

Scanner finds tokens in a word with regexp If word does not match regexp returns an empty Array

Attributes

regexp[R]
word[R]

Public Class Methods

new(word, regexp) click to toggle source
# File lib/textoken/scanner.rb, line 7
def initialize(word, regexp)
  @word       = word
  @regexp     = regexp
  check_types
end

Public Instance Methods

result() click to toggle source
# File lib/textoken/scanner.rb, line 13
def result
  scan = word.scan(regexp)
  scan.length > 0 ? partition(scan, word) : nil
end

Private Instance Methods

check_types() click to toggle source
# File lib/textoken/scanner.rb, line 27
def check_types
  return if word.is_a?(String) && regexp.is_a?(Regexp)
  Textoken.type_err("#{regexp} should be Regexp and #{word}
    has to be a string.")
end
partition(scan, word) click to toggle source
# File lib/textoken/scanner.rb, line 20
def partition(scan, word)
  scan.each do |p|
    word = word.gsub(p, " #{p} ")
  end
  word.split(' ')
end