class Rote::ExtHash

Special type of Hash that uses Regexp keys and maintains insertion order. When searching for a string, the first match (of either kind) is used. Allows backreferences from the key match to be used in the value with $1..$n notation in val str.

Entries are kept in insertion order. Searches/insertion are slow, iteration is constant time. It’s basically an unbucketed hash.

Public Class Methods

new(map = nil) click to toggle source

Create a new RxHash, copying the supplied map (in random order).

# File lib/rote/rotetasks.rb, line 73
def initialize(map = nil)
  @data = []
  map.each { |k,v| self[k] = v } if map

Public Instance Methods

[](key) click to toggle source

Fetch the first matching data.

# File lib/rote/rotetasks.rb, line 88
def [](key)
  md = nil
  if v = @data.detect { |it| md = /^#{it[0]}$/.match(key.to_s) }
    v[1][0].gsub!(/\$(\d)/) { md[$1.to_i] }
[]=(key,value) click to toggle source

Insert the given regex key unless it already exists. You may use string representations for the keys, but they are converted as-is to regexps.

Returns the value that was inserted, or nil.

# File lib/rote/rotetasks.rb, line 83
def []=(key,value)
  @data << [key,value] unless member?(key)      
fetch_entry(key) click to toggle source

Fetch a single entry based on key equality.

# File lib/rote/rotetasks.rb, line 97
def fetch_entry(key)
  @data.detect { |it| it[0] == key }
member?(key) click to toggle source

Determine membership based on key equality.

# File lib/rote/rotetasks.rb, line 102
def member?(key)
  true if fetch_entry(key)