Skip to content

Instantly share code, notes, and snippets.

/hash_lookup Secret

Created September 15, 2016 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/29753dcf041fe62ca984c1ba133fe450 to your computer and use it in GitHub Desktop.
Save anonymous/29753dcf041fe62ca984c1ba133fe450 to your computer and use it in GitHub Desktop.
{
a: { id: 1, name: 'abc' },
b: { id: 2, name: 'def' }
}
@baweaver
Copy link

baweaver commented Sep 15, 2016

path_hash = Hash.new { |h,k|
  if k == '*'
    -> matcher {
      if matcher.is_a?(Hash)
        match_key, match_value = matcher.first
        h.select { |_, sub_hash| match_value === sub_hash[match_key] }
      else
        h.select { |_,v| v[k2] }
      end
    }
  else
    h[k] = Hash.new(&h.default_proc)
  end
}

path_hash[:a] = { id: 1, name: 'abc' }
path_hash[:b] = { id: 2, name: 'def' }
path_hash['*'][{name: 'def'}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment