I tore my ham string two years ago in the gym doing leg presses (off season). The physical therapist placed the blame squarely on not stretching.
The PT said you need to stretch AFTER exercise moreso than before; exercise causes muscles to remain slightly contracted (tight) and stretching causes them to relax again. Now I stretch after every ride. In the gym I warm up, do a light stretch, lift weights, then do a full stretch.
Rob