Skip to content

Instantly share code, notes, and snippets.

@brhoades
Last active January 17, 2017 20:59
Show Gist options
  • Save brhoades/a8032de985ec712f05ad37ddb2261827 to your computer and use it in GitHub Desktop.
Save brhoades/a8032de985ec712f05ad37ddb2261827 to your computer and use it in GitHub Desktop.
Regex key -> value hash: https://glot.io/snippets/em9zobxd1f
class MyHash
def initialize
@keys = []
@values = []
end
def [] index
@keys.each_with_index do |v, i|
if index =~ v
return @values[i]
end
end
nil
end
def []=(key, value)
@keys << key
@values << value
end
end
somehash = MyHash.new
somehash[/someregex/i] = 2
puts somehash["somestring"].inspect # nil
puts somehash["somEregexlalalala"].inspect # somevalue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment