Two suggestions (or variations on the same theme):
The idea is, do what you were going to do anyway, and when his stuff gets in the way of it, say simply that you're concentrating on X and please, you need quiet to focus on your work. If needed, repeat this again (and again) without elaboration, until heard.
It could be work you get back to, or a meeting, or that toilet break you suddenly needed, or the pencil you promised to return (or phone call you promised to make) by 3.26pm. Whatever :)
If he tries it during an interaction with you, do the same but slightly modified ("Now I need to get going and work on these").
Your question relates to many other questions here, about dealing with a person who doesn't stop talking and you need to break the conversation. The simple version is, do what you need to do, as if he's agreeing and happy with it, if he tries to draw you back into it, just say "Sorry, I need to X", and say it as a statement, not a tone of voice that says "... please say it's okay". He won't agree, so just act as if he did. He probably won't chase you or call you on it, if he does just repeat that you need to do X. If he says you don't, explain (find a reason!) why you do.
I've used this on angry ranters for years. Stay cool, don't attack them, make clear you are doing your stuff, minimise any reward they get of you stopping to pay attention. Continue making coffee at the same time if it's in the office kitchen, then leave with it as one usually does. Sit down and work if he's behind you, and if he's still there in 2 minutes just say "sorry, I need quiet to do this, its hard to listen and be sure I'm doing my job properly." If its after work at the pub, there is surely someone else you need to be in a conversation with.
If all else fails, pull out the phone, just say "excuse me" without making it a question, and dial a friend or someone you can make a work call to. Once answered, pay all attention to the phone, and after a few seconds drift away from him to hear the call more clearly.....
Basically this is denying him the reward (attention) without being rude. It works very well.
Its important to "just say it". Almost a monotone, like you're saying "here's the paper" or whatever. Not an emotional thing. That's an important part of it. It just happens that you have to work (or whatever), or that you're busy when he's ranting. Keep the tone neutral and the words brief, don't elaborate or get sucked into justifying, don't ask or imply you are asking anything, if he doesn't act better just repeat identically without any more detail or emphasis, and then try to carry on again. If he repeats, you pause, repeat same words (not adding anything) then turn back to your work having given him barely a second or two. Repeat as long as he repeats. (It'll win fairly fast in almost all cases, because it gives no attention but also, no justification or pretext to escalate on his part either).
As an aside, demanding attention without regard to whether its wanted, is a feature in many forms of abuse; abuser tactics often include demanding or wheedling attention, or guilt/anger/different tactics to get it. Look that aspect up as well, because it may be others do it and you might want to be aware if so.