I love Ruby’s syntax for method calls. The syntax for calling dynamic methods is pretty nice, and it’s easily mastered.
send method is one of the best and most basic ways to call a method dynamically on your object. Let’s say we have an object with multiple methods like this:
1 2 3 4 5 6 7
The method you want to call might be represented as a string or a symbol. Your method call might look like this:
1 2 3
send with parameters
Now, some of your methods might have parameters. How are those called? Simply pass the argument values into the
send call as the final argument, which is a variable length argument:
You can also pass the parameters as a splat array, such as this:
In Ruby, the splat operator destructures the array into parameters, so the above code essentially becomes:
send turns out to be pretty easy to deal with and really useful. Use it, and be happy.