Balanced Bracket method in Ruby