Blocks with Parameters
Explore how Ruby blocks accept parameters from objects executing them and learn to use these parameters effectively. Understand best practices for naming variables and managing counts starting at zero. Gain practical skills by improving a savings program with string interpolation and adding monthly revenue calculations to a Magic Piggy Bank application.
We'll cover the following...
Passing parameters
The object that executes our block can pass a parameter. This depends on the implementation. Some objects do pass parameters to our blocks, while others don’t. It’s up to the programmer if they want to use this parameter or ignore it. So far, we’ve ignored this information but, technically, the parameter was always there. Let’s see how we can use it
In the case below, we have the object 24 with type integer.
Let’s write a program called “Piggy Bank,” which will accept 500 dollars every month and save that money. ...