Skip to content

Instantly share code, notes, and snippets.

@qickstarter
Created February 16, 2014 07:11
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 qickstarter/9030423 to your computer and use it in GitHub Desktop.
Save qickstarter/9030423 to your computer and use it in GitHub Desktop.
test = {
/^a$/ => 'aa',
/^b+/ => 'bb',
/^c+$/ => 'cc',
}
test_array = test.to_a
keys = test.keys.map.with_index { |v, index| "(#{v.source})" }
matcher = Regexp.new(keys.join('|'))
if matcher =~ 'ccc'
index = $~[1..-1].rindex($~[0])
p test_array[index][1] #=> 'cc'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment