You're invoking a method on a nil object causing a runtime error (i.e the nil class does not define a method 'traverse'). Try checking the object first before calling the method. Something simple like: unless node.nil? shouldo it. In 7stud's example, the variable 'data' is nil so calling a method on it, in this case 'each', raises the same error.