Last active
January 17, 2017 20:59
-
-
Save brhoades/a8032de985ec712f05ad37ddb2261827 to your computer and use it in GitHub Desktop.
Regex key -> value hash: https://glot.io/snippets/em9zobxd1f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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