Dynamic method calls
You have just discovered that the staff you have been carrying around your entire life also turns into a glider. But you don't really know how to fly. Fortunately, your previous incarnations speak in your head telling you what to do. You just have to do what they tell you to do.
Your first instinct would probably be to build some sort of if-else logic. e.g. If you knew that the actions possible were lift or bank you could very well just do what's written below.
That solution only works as long as you know exactly what methods are going to be called. What if you didn't? What if you did not know all the actions that were possible on the glider object? If only there was a generic way to write the above.
Ruby gives a convenient way for you to call any method on an object by using the send
method. send
takes, as its first argument, the name of the method that you want to call. This name can either be a symbol or a string.
Additionally, now some of these actions require further arguments. e.g. The command to 'bank' takes a string of either 'left' or 'right', while the command to 'roll' takes the number of degrees it should dive for. Again, send
gives us a convenient way to pass arguments to these methods. All further arguments to send
after the first (which you'll recall has the name of the method you want called itself) become arguments that get passed down to that method.