Constraining gradients

The online manual explains the basic use of the gradient tool, but how does one constrain a gradient to an object as opposed to spilling it over the entire canvas?

Setting an object’s blend method to “onto” means that it shades in everything that is below it, but doesn’t cover the whole canvas. For example, to have a gradient-shaded rectangle, one would need to create a gradient layer above a rectangle layer, then set the gradient’s blend method to “Onto”, and finally encapsulate the two layers.

See here: