Class: CTioga2::RegexpHash
- Inherits:
-
Object
- Object
- CTioga2::RegexpHash
- Defined in:
- lib/ctioga2/utils.rb
Overview
This class implements a Hash whose values can also be retrieved by pattern matching.
Instance Attribute Summary collapse
-
#hash ⇒ Object
Hash for non regexp keys.
-
#regexp_hash ⇒ Object
Hash for regexp keys.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Gets the value corresponding to the key, using pattern matching should the need arise.
-
#[]=(key, value) ⇒ Object
Sets the key to the given value.
-
#initialize ⇒ RegexpHash
constructor
A new instance of RegexpHash.
- #keys_for(value) ⇒ Object
Constructor Details
#initialize ⇒ RegexpHash
Returns a new instance of RegexpHash.
420 421 422 423 |
# File 'lib/ctioga2/utils.rb', line 420 def initialize() @hash = {} @regexp_kv = [] end |
Instance Attribute Details
#hash ⇒ Object
Hash for non regexp keys
415 416 417 |
# File 'lib/ctioga2/utils.rb', line 415 def hash @hash end |
#regexp_hash ⇒ Object
Hash for regexp keys
418 419 420 |
# File 'lib/ctioga2/utils.rb', line 418 def regexp_hash @regexp_hash end |
Instance Method Details
#[](key) ⇒ Object
Gets the value corresponding to the key, using pattern matching should the need arise.
If there are several regexps matching a given key, the implementation guarantees that the last one to have been inserted that matches is taken
440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/ctioga2/utils.rb', line 440 def [](key) if @hash.key?(key) return @hash[key] else for k,v in @regexp_kv.reverse if k === key return v end end end return nil end |
#[]=(key, value) ⇒ Object
Sets the key to the given value
426 427 428 429 430 431 432 |
# File 'lib/ctioga2/utils.rb', line 426 def []=(key, value) if Regexp === key @regexp_kv << [key, value] else @hash[key] = value end end |
#keys_for(value) ⇒ Object
453 454 455 456 457 458 459 460 461 |
# File 'lib/ctioga2/utils.rb', line 453 def keys_for(value) ret = [] for k,v in @hash if value == v ret << k end end return ret end |