Never on a ride, but when I was walking my dog once, another dog cam charging up to attack (or play, it was hard to tell). I raised my arms and shouted "NO! GO HOME! GO HOME" The dog put its tail between its legs and went home. Most dogs aren't looking for a real altercation and quickly change their minds when they find out you're not playing.
The dog you encountered was probably trying to play (not that it would hurt any less should he take you down). Had the dog been genuinely trying to attack you, it would probably have ducked and planned another lunge. For that reason, I wouldn't mess around with a dog, but rather stop the bike and place it between myself and the dog just in case.
__________________
It's like riding a bicycle