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.
549 550 551 552 |
# File 'lib/ctioga2/utils.rb', line 549 def initialize() @hash = {} @regexp_kv = [] end |
Instance Attribute Details
#hash ⇒ Object
Hash for non regexp keys
544 545 546 |
# File 'lib/ctioga2/utils.rb', line 544 def hash @hash end |
#regexp_hash ⇒ Object
Hash for regexp keys
547 548 549 |
# File 'lib/ctioga2/utils.rb', line 547 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
569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/ctioga2/utils.rb', line 569 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
555 556 557 558 559 560 561 |
# File 'lib/ctioga2/utils.rb', line 555 def []=(key, value) if Regexp === key @regexp_kv << [key, value] else @hash[key] = value end end |
#keys_for(value) ⇒ Object
582 583 584 585 586 587 588 589 590 |
# File 'lib/ctioga2/utils.rb', line 582 def keys_for(value) ret = [] for k,v in @hash if value == v ret << k end end return ret end |