Recording with a light-second delay from basecamp at Mt. Ruby where all the rocks are gems and all the gems are mountains aka the origami enchanted forrest aka the blank screen wrecking yard aka the wind sock between your ears aka the Grace Hopper appreciation society aka the Sandi Metz support group aka the solar-powered confidence summer jam aka The its-not-about-computers School aka the Flatiron School
‘yield’ is a keyword that appears in a method to turn over the reigns to a yet-to-be-determined block of code. You add the block when you call the function. It can be any block you like.
Trivial examples look like this:
-OR- With a method that takes an argument:
These are trivial examples because they could be re-written with no added key-strokes as:
The interesting implementions of the ‘yield’ keyword involve methods with more logic. For example:
That’s ‘yield’ in action. ‘yield’ allows you to, on the fly, add a bit of custom logic to your methods. It might feel like a slippery concept because it is. For people that learn from metaphors I came up with 5 that might help:
1. ‘yield’ is the settings on your microwave. Like on mine i have “time defrost”, “auto defrost”, “reheat”, “cook”, “popcorn”, “pizza”, and “beverage.”
2. ‘yield’ is the attachments on your kitchenaid food processor.
3. ‘yield’ is AutoTune.
4. ‘yield’ is tinted sunglasses.
5. ‘yield’ is that trick play from Little Giants.
Make yourself useful.