Class: RichHash
- Inherits:
-
Object
- Object
- RichHash
- Defined in:
- lib/boyermoore.rb
Overview
Hash impersonator that accepts regular expressions as keys. But the regular expression lookups are slow, so don’t use them (unless you have to).
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
-
#initialize ⇒ RichHash
constructor
A new instance of RichHash.
Constructor Details
#initialize ⇒ RichHash
Returns a new instance of RichHash.
6 7 8 9 |
# File 'lib/boyermoore.rb', line 6 def initialize @regexps = {} @regular = {} end |
Instance Method Details
#[](k) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/boyermoore.rb', line 11 def [](k) regular = @regular[k] return regular if regular if @regexps.size > 0 @regexps.each do |regex,v| # linear search is going to be slow return v if regex.match(k) end end nil end |
#[]=(k, v) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/boyermoore.rb', line 22 def []=(k,v) if k.kind_of?(Regexp) @regexps[k] = v else @regular[k] = v end end |