Module: WithReverseLookup

Defined in:
lib/with_reverse_lookup.rb

Overview

Useful for defining a hash a single way and being able to look up by key or value. For instance:

hash = {
  1 => :foo,
  2 => :bar
}.extend(WithReverseLookup)

hash[1] #=> :foo
hash[:foo] #=> 1

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/with_reverse_lookup.rb', line 14

def [](key)
  if self.keys.include?(key)
    super
  else
    return self.index(key)
  end
end