I am processing 2-dimensional shapes in ruby. I have a Point class with x and y attribute, and a Line class which has two end points. Later in the process chain, I often want to identify and process vertical lines slightly differently from horizontal lines, even though both can occur in the same set of Lines. I have mix-ins to do the processing, but often I find the method has something like
if l.vertical?
l.do_something_with_y_coordinate
elsif l.horizontal?
l.do_something_with_x_coordinate
end
I see this pattern quite frequently and I do not like the repetition. I could try to avoid it by identifying instances of Line that are vertical and creating a VerticalLine class, and a HorizontalLine class, but that feels more like .Net than ruby and does not reduce the duplication, just encapsulates it.
Another way might be to have a direction method that returns :x or :y and then a method to "do_something_with direction".
Does anybody else have any suggestions about how to refactor?