Class: Hash

Inherits:
Object show all
Defined in:
lib/sugar-high/blank.rb,
lib/sugar-high/hash.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#hash_revertObject



13
14
15
16
17
# File 'lib/sugar-high/hash.rb', line 13

def hash_revert
  r = Hash.new {|h,k| h[k] = []}
  each {|k,v| r[v] << k}
  r
end

#rewrite(mapping) ⇒ Object



6
7
8
9
10
11
# File 'lib/sugar-high/hash.rb', line 6

def rewrite mapping
  inject({}) do |rewritten_hash, (original_key, value)|
    rewritten_hash[mapping.fetch(original_key, original_key)] = value
    rewritten_hash
  end
end

#try_keys(*keys) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/sugar-high/hash.rb', line 19

def try_keys *keys
  option = last_option keys
  keys.flatten.each do |key|
    return self[key] if self[key]
  end
  return option[:default] if option[:default]
  nil
end