From: Mark Hahn Date: 2001-11-29T22:00:30+09:00 Subject: [ruby-talk:26886] Can this be factored? The two if statements below are identical except the words left and right (and < & >) are reversed. I don't think this can be factored but I thought I'd throw this out to see if anyone can think of a way. I thought maybe some sort of text pre-processing could help, but then I realized the resulting code would be the same. def find key, countIn if key < @key @leftCount += 1 if @left nextNode = @left; nextCount = @leftCount matchCount = countIn-@leftCount-@rightCount leftMatchCount = @leftCount-@left.rightCount-@left.leftCount if 3*@rightCount+2*matchCount+1*leftMatchCount+2*@left.leftCount < 2*@rightCount+1*matchCount+2*leftMatchCount+3*@left.leftCount @left.right,@left,nextCount,@leftCount,@left.rightCount = self,@left.right,countIn,@left.rightCount,countIn-@leftCount+@left.rightCoun t end return nextNode.find(key,nextCount) end end if key > @key @rightCount += 1 if @right nextNode = @right; nextCount = @rightCount matchCount = countIn-@leftCount-@rightCount rightMatchCount = @rightCount-@right.leftCount-@right.rightCount if 3*@leftCount+2*matchCount+1*rightMatchCount+2*@right.rightCount < 2*@leftCount+1*matchCount+2*rightMatchCount+3*@right.rightCount @right.left,@right,nextCount,@rightCount,@right.leftCount = self,@right.left,countIn,@right.leftCount,countIn-@rightCount+@right.leftCou nt end return nextNode.find(key,nextCount) end end return self end