Skip to content

Instantly share code, notes, and snippets.

def brackets_valid?(str)
open = []
matches = { '{' => '}', '(' => ')', '[' => ']' }
str.split('').each do |c|
if matches.keys.include? c # it's an opening bracket
open.push c
elsif matches.values.include? c # it's a closing bracket
# must close the last open bracket
return false unless c == matches[open.pop]