Is it safe to masturbate if you have ringworm on your hands?

Would not do it. Ringworm on the hand is called tinea manum. It should be treated aggressively to prevent spread to other parts of the body. I would refrain from genital contact and masturbation until under treatment and recommendation by your doctor.
Treat the ringworm. As long as you are applying antifungal medications to the lesions of your hands, washing and thoroughly drying the areas touched, you are at minimal risk of spreading this to other areas of your body.