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.

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.



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

def initialize default = false, caching = false
  if caching
    super(default, false) do |h, k|
      h[k] = h.fetch k.downcase, default
    end
  else
    def self.[] key  # :nodoc:
      super(key.downcase)
    end
  end
end

Instance Method Details

#add(words, kind = true) ⇒ Object

Add words to the list and associate them with kind.



114
115
116
117
118
119
# File 'lib/coderay/helpers/word_list.rb', line 114

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