loops - closed parenthesis ruby method -


can explain making method exit loop after first pair?

  def closed_parens(string)     chars = string.split(//)     chars.each_with_index |c, i|      if c == "("        chars.shift       if chars[0] != ")"         return false        else          chars.shift        end       elsif c == "{"        chars.shift        if chars[0] != "}"         return false        elsif          chars.shift       end       elsif c == "["        chars.shift        if chars[0] != "]"         return false        else          chars.shift        end       end    end     if chars.length > 0      return false    else      return true    end  end   closed_parens("{}{}") 

you don't want use shift on enumerable object while iterating on it.

you may want enumerable#each_cons method like

[your enumerable item].each_cons(2) |current_item, next_item|    # work... end 

Comments