There’re two differences:
-
applyaccepts an instance as the receiver whilewithrequires an instance to be passed as an argument. In both cases the instance will becomethiswithin a block. -
applyreturns the receiver andwithreturns a result of the last expression within its block.
I’m not sure there can be some strict rules on which function to choose. Usually you use apply when you need to do something with an object and return it. And when you need to perform some operations on an object and return some other object you can use either with or run. I prefer run because it’s more readable in my opinion but it’s a matter of taste.