Skip to content

Instantly share code, notes, and snippets.

@Schwad
Created June 8, 2016 10:53
Show Gist options
  • Save Schwad/d6f891b0753e1141dc6a6400bd1f2c14 to your computer and use it in GitHub Desktop.
Save Schwad/d6f891b0753e1141dc6a6400bd1f2c14 to your computer and use it in GitHub Desktop.
def matching_brackets?(arg)
opener_symbols = ["{", "(", "["]
closer_symbols = ["}", ")", "]"]
openers = Hash.new(0)
closers = Hash.new(0)
openers['('] = arg.scan(/\(/).length
openers['['] = arg.scan(/\[/).length
openers['{'] = arg.scan(/\{/).length
closers[')'] = arg.scan(/\)/).length
closers[']'] = arg.scan(/\]/).length
closers['}'] = arg.scan(/\}/).length
if openers['('] != closers[')']
return false
end
if openers['{'] != closers['}']
return false
end
if openers['['] != closers[']']
return false
end
return true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment