Class: CodeRay::CaseIgnoringWordList

Inherits:
WordList
  • Object
show all
Defined in:
lib/coderay/helpers/word_list.rb

Overview

A CaseIgnoringWordList is like a WordList, only that keys are compared case-insensitively.

Ignoring the text case is realized by sending the downcase message to all keys.

Caching usually makes a CaseIgnoringWordList faster, but it has to be activated explicitely.

Defined Under Namespace

Modules: Uncached

Instance Method Summary collapse

Constructor Details

#initialize(default = false, caching = false) ⇒ CaseIgnoringWordList

Creates a new case-insensitive WordList with default as default value.

You can activate caching to store the results for every [] request. This speeds up subsequent lookups for the same word, but also uses memory.



103
104
105
106
107
108
109
110
111
112
# File 'lib/coderay/helpers/word_list.rb', line 103

def initialize default = false, caching = false
  if caching
    super(default, false) do |h, k|
      h[k] = h.fetch k.downcase, default
    end
  else
    super(default, false)
    extend Uncached
  end
end

Instance Method Details

#add(words, kind = true) ⇒ Object

Add words to the list and associate them with kind.



121
122
123
124
125
126
# File 'lib/coderay/helpers/word_list.rb', line 121

def add words, kind = true
  words.each do |word|
    self[word.downcase] = kind
  end
  self
end