class CTioga2::RegexpHash

This class implements a Hash whose values can also be retrieved by pattern matching.

Attributes

hash[RW]

Hash for non regexp keys

regexp_hash[RW]

Hash for regexp keys

Public Class Methods

new() click to toggle source
# File lib/ctioga2/utils.rb, line 549
def initialize()
  @hash = {}
  @regexp_kv = []
end

Public Instance Methods

[](key) click to toggle source

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

# 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) click to toggle source

Sets the key to the given value

# 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) click to toggle source
# 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